Question

I'm fairly new to IoC and am a little stuck on the best practice going forward. The project I'm working on is built in MVC with Sharp Architecture. The way we currently use IoC is to specify the repository interfaces in the constructor of each controller and let Castle Windsor do the rest and pass these instances into each model. However as the controllers may be instantiating many new models, which in turn call other models, etc it can easily get out of hand and some controllers now have 20+ repository interfaces in the constructor parameters.

As each controller inherits a BaseController I was thinking of making a repository repository, that was just a list of repositories with a Get() method that returns the repository of type T, and this could be instantiated/populated in base controller and just passed into each model then we wouldn't need to worry about all the repositories being passed around.

However this feels wrong some how, and I can't help but feel Castle Windsor should do something like this already, but can't figure out how.

Would love to hear your thoughts on this.

Thanks.

Was it helpful?

Solution

A repository of repositories, that sounds like the Unit of Work pattern. What you can do is inject an IUnitOfWorkFactory that allows you to create new IUnitOfWork instances. A unit of work instance than holds all the repositories.

Look for an example of such implementation here.

Personally, I wouldn't inject repositories or even unit of work instances into a controller, but inject a service into the controller. The service will than have the unit of work or repositories as dependency.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top