views:

364

answers:

1

I have decided to use ASP.NET MVC for a website project and want to follow some of the best practices being written about.

So, I have separated out the Domain/Model into a separate project, created IRepositories and concrete repositories and have now turned my attention to Castle Windsor as the IoC.

The problem I now face is that for a particular Controller, on the constructor I will now have to pass in multiple IRepository parameters.

My questions are:

  1. Have I perhaps created too many Repositories - in general, I map 1 repository to 1 entity class to 1 database table. Should my Repositories be containing effectively more than one entity/db table?
  2. Have I missed the point with IoC's and Dependency Injection, and should I not be concerned with how params are passed into a Controller constructor?

To give some sort of context. Part of the website will show a google map of properties that is searchable by property type (Castle, House, Pub etc), location (postcode, city), opening time etc. So, these searchable components are all separate entities PropertyType, Address.City, Address.Postcode.Lat+Long, OpeningTime.DateTime. Therefore, there are also 3 separate repositories that have to be passed into the SearchController constructor.

This is a simple example but I can envisage many more repository parameters being passed into other controllers in the future.

Hope all this makes sense.

Thanks for any answers or advice.

+2  A: 

I wouldn't be concerned with how many parameters you're passing into your constructor the IoC will handle all the logic from that.

If your controller ends up with too many parameters, I would look at breaking up the functionality of your controllers, or move the logic into a service class maybe.

As for the Repositories, I usually go with a Generic Repository that would take a single Entity in its implementation. Then I have a service class that aggregates that information into logical units. In this scenario your controller only needs access to the service. For example:

interface IRepository<T>
{
    IQueryable<T> GetAll();
    T GetOne(int id);
    void Save(T item);
    void Delete(T item);
}

class OrderService
{
    public OrderService(IReopository<Order> orderRepository, IRepository<OrderDetail> orderDetailRepository, IRepository<Payment> paymentRepository, etc) { }

    public Order CreateOrder(List<OrderDetails> details)
    {}
    // .. other aggregate methods
}
bendewey
Thanks for the answer. 2 follow on questions please: 1) When you say not to be concerned with the number of params on controller constructor - I still have to actually code up the constructor though yes? The IoC won't behind the scenes actually create the code for the constructor will it? 2) Presumably, as the Service class contains only interfaces, then to pass it into the Controller as a class rather than an interface (IOrderService) is OK, and still allows for a seperation of concerns and allows for easy testing? Thanks again
glasto red
1) Yes, You will still have to setup the fully loaded constructor. 2) You container will handle what class instance to use when loading the interfaces. See this recent question, I wrote two answers that might help. http://stackoverflow.com/questions/871405/why-do-i-need-an-ioc-container-as-opposed-to-straightforward-di-code
bendewey