Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6074387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:22:36+00:00 2026-05-23T10:22:36+00:00

I have the following code: public abstract class BaseController : Controller { public IUserService

  • 0

I have the following code:

public abstract class BaseController : Controller
{
    public IUserService UserService { get; set; }
}

All my controllers inherit from this base controller. I started out by configuring it in Ninject using the following code:

kernel.Bind<BaseController>()
      .ToSelf()
      .WithPropertyValue("UserService", x => x.Kernel.GetService(typeof(IUserService)));

This did not work. I assume it is because of the fact that the BaseController is an abstract class (please confirm my assumption). So I moved on to modify the configuration to:

kernel.Bind<HomeController>()
      .ToSelf()
      .WithPropertyValue("UserService", x => x.Kernel.GetService(typeof(IUserService)));

This does work. The minor downside is that I now have to configure every controller the same way.

Since I also have DependencyResolver setup in my ASP.NET MVC 3 project I could also remove the above Ninject configuration and modify my base controller to look like:

    public IUserService UserService
    {
        get
        {
            return DependencyResolver.Current.GetService<IUserService>();
        }
    }

Is there any benefit to using the fluent configuration as opposed to using the DependencyResolver approach? Is one better than the other? Which approach would be considered a better practice?

It is worth mentioning that I did not want to do constructor injection in my base controller.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:22:36+00:00Added an answer on May 23, 2026 at 10:22 am

    A better practice in MVC it is to use constructor injection over property injection. Why did you make your choice like this ?

    Using Constructor Injection you states that all dependencies in constructor are necessary for the class to do its job.

    Property injection means that the dependencies are optional or that there are the local defaults implementations, so all will work even if you don’t provide necessary implementations yourself.

    You should really know what you’re doing using Property injection or you have no other choice, so the safer approach is to rely on constructor injection.

    Now I’ll give you my point of view. Other may have other opinions.

    DependencyResolver was introduced in MVC 3 for “convenient” service location but for me it’s a regular Service locator which for me is also an anti-pattern http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx. I don’t use it because I don’t like it and there is no benefit in using it.
    I prefer to user my controller factory like before and pass the dependencies through constructor.

    More the IDependencyResolver has somme issues with some IoC containers (I don’t know if it’s the case with Ninject). You can read more here : http://mikehadlow.blogspot.com/2011/02/mvc-30-idependencyresolver-interface-is.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base controller abstract class, which my other controllers inherit. This class
I have following code: public abstract class TestProperty { public abstract Object PropertyValue {
I have a question about .net generics. Consider the following code: public abstract class
Consider the following code: public abstract class Test1 { public object Data { get;
i have wrote the following polymorphic code: public abstract class A { readonly int
I have the following code: abstract class BaseLpmServiceForm extends BaseFormPropel { public function setup()
I have the following snippet of code. abstract class MrParent { public function __construct()
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code: public class Test { public static void Main() {
I have the following code in my controller: public ActionResult Details(int id) { var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.