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. :)

3
  • 4
    It sounds like there's something else being used here besides out-of-the-box ASP.NET MVC. Maybe a dependency injection framework? Commented Sep 11, 2013 at 16:42
  • Check the references, you may have something like Ninject ? Commented Sep 11, 2013 at 16:43
  • See also asp.net/mvc/tutorials/hands-on-labs/… Commented Sep 11, 2013 at 17:10

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

OK, but how would that be shoehorned into an MVC controller where there is the expectation of a parameterless constructor?
@RobertHarvey this is where we use dependency injection tools such as Ninject and StructureMap. On app start they register the dependencies.
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, I'm just not clear where or how the injection is occurring.
Check your global.asax it would have a block similar to what is mentioned here
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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.