views:

121

answers:

4

I have a many-to-many assocition between an Employee and a Team. Foreach entity I have a repository.

Now I use ASP.NET MVC and I created a EmployeeController. I also created a View to edit an Employee. For this view I need a DropDownList with all Teams. The problem is that my EmployeeController only has got EmployeeRepository. So how can I get all Teams? My solution now is to use two repositories.

But is this a good solution?

Could I instead create TeamController and write a method returning all Teams (how would I do that)?

+6  A: 

Yes, it's perfectly acceptable for a controller to have references to two repositories.

In fact, my controllers work with multiple repositories more often than not.

Jim G.
A: 

Typically you would create a repository for the aggregate root. The repository would have methods returning the entities populated for the controller.

If the entities are totally unrelated in your domain model you may want to create a service wrapping the two separate repositories to get the data you need.

blu
who is the aggregate root in a many-to-many association :)?In my domain model I have a bidirectional many-to-many association. I ask myself what benefits I would have when I create a unidirectional relation instead. The Employee would than the aggregate root. The Team has only one relation to the Employee so I think this would be ok.
Rookian
Agreed, when faced with the same issue I have gone with a domain service to wrap up the repositories.
blu
My suggestion is 1: see if there is logically an aggregate root in terms of the domain rules. If not, 2: use a domain service. Other people have suggested services with + votes, so this being downvoted is questionable.
blu
downvoting with no comment sucks >< (!)
Rookian
+2  A: 

Wanna go smart? Do not use repositories inside controllers whatsoever. Instead use Domain Services. It doesn't sound so bad when you think one controller integrates the work of many services doesn't it?

Mikeon
+3  A: 

It's quite OK, but once you feel that the Controller becomes too cluttered, you can refactor its dependencies to an Aggregate Service.

Mark Seemann