1

Right now I am learning MVC, but have running into a problem that I can't figure out.

In a test project I have downloaded I can see that some controllers doesn't have a parameterless constructor, instead they have a constructor that has many parameters. How is that possible?

I mean, when someone visit a page a controller must be created first. The thing that creates the controller must create it with the parameterless constructor.

I guess that it is something that I have missed in the MVC. :)

tereško
  • 58,060
  • 25
  • 98
  • 150
Delta
  • 171
  • 11

2 Answers2

4

Without seeing the code, my first thought is that there is some dependency injection involved (i.e. Ninject, Autofac, etc...) which will inject a constructed object (from rules defined) into the constructors. If you are unfamiliar with the Inversion of Control and Dependency Injection patterns, here is a stack over flow question that has a pretty clear explanation: What is Inversion of Control?

If something else is going on, then perhaps you could include some code so we could take a look at it.

Community
  • 1
  • 1
Donovan R
  • 378
  • 3
  • 8
4

This is known as constructor injection, which is used as the basis of dependency injection tools like Ninject and StructureMap. It allows for the loose coupling of objects.

Essentially whenever you have to create an instance of an object using new you are tightly coupling yourself to a concrete implementation of that object. This can make things difficult if not impossible for testing.

void MakeBacon()
{
    var smokey = new Bacon();
}

By using injection methods such as constructor or dependency injection we are loosely coupling ourselves as we no longer have to create a new instance, it is injected instead.

void MakeBacon(IBacon smokey){
   // Make bacon here.
}

Here is a good answer on stack exchange explaining constructor injection

What is constructor injection?

And here is a Microsoft article on constructor injection

Annotating Object for Constructor Injection

Community
  • 1
  • 1
Colin Bacon
  • 15,436
  • 7
  • 52
  • 72
  • OK, but how would that be shoehorned into an MVC controller where there is the expectation of a parameterless constructor? – Robert Harvey Sep 11 '13 at 16:58
  • 1
    @RobertHarvey this is where we use dependency injection tools such as Ninject and StructureMap. On app start they register the dependencies. – Colin Bacon Sep 11 '13 at 17:06
  • I guess I'm not making myself clear. I'm talking about the specific case of ASP.NET MVC controllers, not the general case of DI proper. How do you inject into a controller if the MVC infrastructure is expecting a parameterless constructor? It obviously [can be done](http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection#Exercise1), I'm just not clear where or how the injection is occurring. – Robert Harvey Sep 11 '13 at 17:07
  • 1
    Check your global.asax it would have a block similar to what is mentioned [here](http://stackoverflow.com/a/126905/1664852) – Praveenram Balachandar Sep 11 '13 at 17:14
  • Thanks Colin Bacon, really good answer! The project was using StructureMap. Now I understand how that was possible. – Delta Sep 11 '13 at 17:32
  • 1
    The more recent versions of MVC support Dependency Injection natively, you just have to register your resolver using `DependencyResolver.SetResolver()` when your app starts up. It will then try to resolve constructor dependencies using that resolver. – Nick Albrecht Sep 11 '13 at 20:21
  • Looks like it's not possible to implement via DependencyResolver.SetResolver() because you get this exception. "The type System.RuntimeType does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator." So here we have a negative example of the whole DI/IoC "framework" = hard coded references to type definitions which are not globally available. Bad design, no other word for it. It is not possible for anyone to implement the IDependencyResolver interface without installing the Patterns & Practices dependencies = opposite of IoC, LOL! – Tony Wall Jul 09 '15 at 08:56