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

后端 未结 4 1757
臣服心动
臣服心动 2020-12-31 10:45

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

相关标签:
4条回答
  • 2020-12-31 11:22

    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?

    0 讨论(0)
  • 2020-12-31 11:30

    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.

    0 讨论(0)
  • 2020-12-31 11:31

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

    0 讨论(0)
  • 2020-12-31 11:32

    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.

    0 讨论(0)
提交回复
热议问题