MVC with Scaffolding -Repository Dependency Injection
I am new to MVC and dependency injection so I have a few questions about the scaffolding when making it a Repository:
1) You can not remove the PersonController() c开发者_StackOverflow中文版onstructor otherwise you get an error: No parameterless constructor defined for this object
2) If you did use it as is, wouldn't this tightly couple your repository to the PersonRepository?
CODE EXAMPLE:
// If you are using Dependency Injection, you can delete the following constructor
public PersonsController() : this(new PersonRepository())
{
}
public PersonsController(IPersonRepository PersonRepository)
{
this.PersonRepository = PersonRepository;
}
I'll write a blog post about this. What you'll need to do is register a DI container with MVC 3. For example, using NuGet, you can
Install-Package Ninject.Mvc3
Then in the App_Start folder, there's a file you can add your Ninject Bindings to. Something like:
Bind().To();
Then you can remove the default ctor. If you can't get it to work, I'll try to blog about it soon.
精彩评论