3

My presentation layer is in MVC 3.

I have a data access layer which returns repositories. Each repository is mapped to a single table.

For example, I have the following repository interfaces:

  • ICustomerRepository
  • ICustomerTypeRepository

I have Entity Framework implementations of these repositories. These implementations get injected at a runtime with Ninject.

I have two controllers:

  • CustomerController
  • CustomerTypeController

Each controller has a set of CRUD routines. Customer must have a type.

In order to create a customer record, I need to use EFCustomerTypeRepository.

I now have a constructor within a CustomerController which looks this:

public class CustomerController: Controller
{
   private ICustomerRepository customerRepository;        
   private ICustomerTypeRepository customerTypeRepository; 

   public CustomerController(ICustomerRepository customerRepository, 
                             ICustomerTypeRepository customerTypeRepository)
   {
      this.customerRepository = customerRepository;            
      this.customerTypeRepository = customerTypeRepository;
   }     

...

Development is in early phase, and if I take this approach, then within next few days this constructor will have 5 or more parameters. This doesn't look right.

I could store all this logic in EFCustomerRepository, however, this will break single responsibility principle. So I'm not going to do that.

Finally, I can wrap these repositories and pass a single RepositoryWrapper type object to my controller.

I don't want to introcude any more frameworks, tools than I have to. I'm aiming to achieve loose coupling and easy extensibility, but currently I'm stuck between passing more parameters to a constructor, or writing a wrapper for repositories.

I'm using MVC 3, EF 4.1, Ninject, Moq and Automapper. Thank you

  • 1
    possible duplicate of [Dependency Injection Constructor Madness](http://stackoverflow.com/questions/2420193/dependency-injection-constructor-madness) – Mark Seemann Aug 16 '11 at 15:32

2 Answers2

2

I would say you have to watch for aggregates in your code (from Domain Driven Design point of view). So if you can make a class encapsulate a lot of small other classes for which there's no use storing them by themselves you can create a nice objectgraph and having just a single ICustomerRepository which encapsulates the internal references to for example customer type.

thekip
  • 3,660
  • 2
  • 21
  • 41
  • Ended up writing an aggregate to wrap my repositories into a single service. –  Aug 30 '11 at 09:35
0

Anyway, injecting multiple repositories into a controller constructor isn't necessarily a bad practice. It is called "constructor over-injection", see Mark Seemann's book on this topic.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Alessandro
  • 61
  • 6