开发者

How much to put in a Repository class?

开发者 https://www.devze.com 2022-12-29 21:06 出处:网络
When using the repository pattern is it recommended to have one Repository class for each database table? Would I also map one se开发者_StackOverflow中文版rvice layer class to one repository class. I\

When using the repository pattern is it recommended to have one Repository class for each database table? Would I also map one se开发者_StackOverflow中文版rvice 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!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号