0
votes

I have a very layered MVC3 application (seperate projects for the domain, services, web, infrastructure, etc.)

I understand the concept of controller constructor injection, which MVC3 and Ninject work so kindly together. But what about injection for layers separate from the web layer?

For example, I have a service that relies on a Repository interface. The service is called from the controller, and the service itself will be injected properly by the constructor injection, but what about the repository? How do I inject that?

public class MyService
{
  protected virtual IPersonRepository PersonRepository {get; set;}

  public virtual void UseRepository()
  {
    PersonRepository.FindEveryoneInTheWorldButDontReturnThem();
  }
}

Where/how do I [n]inject the repository in the above example?

Ninject 3.0 Ninject.Web.MVC 3.0

1

1 Answers

3
votes

Inject the repository via constructor injection, and your IoC container (Ninject) will take care of dependency chains (i.e. when you inject MyService as a dependency, any of its dependencies will also be resolved by the container, and their dependencies and so on)

public MyService(IPersonRepository personRepository)
{
    this.PersonRepository = personRepository;
}