Paul
Paul

Reputation: 13

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() constructor 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;
    }

Upvotes: 1

Views: 718

Answers (1)

Haacked
Haacked

Reputation: 59021

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.

Upvotes: 1

Related Questions