3

How is it possible that fresh mvc 5 application just reated from template has dependencies injected into controller?

public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

This what you can see in Account Controller in just created project.

jjaskulowski
  • 2,524
  • 3
  • 26
  • 36

2 Answers2

3

ASP.NET MVC 5 exposes multiple injection points that enable the functionality you described.

The most explicit one is the DependencyResolver. The default dependency resolver just does Activator.CreateInstance(controllerType), so it will throw an error if no public parameterless constructor is selected and will not look for greedy constructors.

However, it's fairly simple to enhance this behavior for your use case.

In your global config, add:

DependencyResolver.SetResolver(
   type => { 
       if(type == typeof(AccountController))
       {
           var userManager   = /* resolve the ApplicationUserManager...*/
           var signInManager = /* resolve the ApplicationSignInManager...*/
           return new AccountController(userManager, signInManager);
       }
       else
       { 
          return Activator.CreateInstance(type)
       }
   },
   type => Enumerable.Empty<object>()
);

This solution passes in delegates rather than an IDependencyResolver implementation, but you could just as easily provide a strongly typed resolver instead.

You probably won't get any real value out of doing your IoC configuration this way, but it should be noted that it is completely possible given the ASP.NET MVC 5 framework. The best practice would be to reference the NuGet package for integrating MVC 5 with your preferred container library and let that do the heavy lifting instead. I personally like StructureMap (check out an example of how to do it this way in the selected answer to this post)

Community
  • 1
  • 1
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • The source for DependencyResolver and the basis for the fallback construction I used in the delegate is available at https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/DependencyResolver.cs – smartcaveman Jun 18 '15 at 03:14
  • 1
    "You probably won't get any real value out of doing your IoC configuration this way". Not everybody would agree with you on this. Some find [Pure DI](http://blog.ploeh.dk/2014/06/10/pure-di/) a very valuable option. – Steven Jun 18 '15 at 19:23
  • @Steven - i'm sure you're right - there's few things that everyone agrees on. Great blog, btw - my statement is actually influenced by stuff I learned from http://blog.ploeh.dk/2012/11/06/WhentouseaDIContainer/ – smartcaveman Jun 18 '15 at 19:36
1

No. I believe the next version will come with an IOC container, but MVC5 does not

dgavian
  • 250
  • 2
  • 10
  • So how does it have this dependencies injected? – jjaskulowski Jun 17 '15 at 22:32
  • 1
    They aren't. You'll notice that AccountController from the template also has a default constructor, which is the one that will get called when the controller is instantiated, and the 2 property getters have null coalesce operators to account for them not being populated/injected. Set a breakpoint in both constructors, then click the log in or register links in the header, and you'll see that only the default constructor gets hit. – dgavian Jun 17 '15 at 22:36
  • Oh, I see. Thank you! – jjaskulowski Jun 17 '15 at 22:39