BestPractices: Is it acceptable to use more than one repository in a MVC-Controller?

血红的双手。 提交于 2019-11-30 09:01:00

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.

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

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?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!