views:

137

answers:

2

When using the repository pattern is it recommended to have one Repository class for each database table? Would I also map one service layer class to one repository class. I'm having a hard time trying to understand how much stuff one repository or service layer class should have.

Thanks!

+3  A: 

You should have a single repository for each class that is a root aggregate in your domain.

Dan
What is a root aggregate?
chobo
you can find some info here: http://stackoverflow.com/questions/1958621/whats-an-aggregate-root
Dan
+2  A: 

Repositories are supposed to be independent of your database structure. They encapsulate all the work of mapping from the database to your domain model and vice versa. That might relate to 1 database table, or it might come from 10 tables, it doesn't matter.

Generally, a single repository corresponds to a single domain model class, but that's not an unbreakable rule either. If you have several domain classes that are very closely-related, especially in an inheritance or containment relationship, then it's not unusual for a repository to deal with more than one distinct type. Still, it's a good idea to try to stick to just one, if you can.

Aaronaught