最佳实践:在 MVC 控制器中使用多个存储库是否可以接受? [英] BestPractices: Is it acceptable to use more than one repository in a MVC-Controller?

查看:22
本文介绍了最佳实践:在 MVC 控制器中使用多个存储库是否可以接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在员工和团队之间建立了多对多关联.Foreach 实体我有一个存储库.

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

现在我使用 ASP.NET MVC 并创建了一个 EmployeeController.我还创建了一个视图来编辑员工.对于这个视图,我需要一个包含所有团队的 DropDownList.问题是我的 EmployeeController 只有 EmployeeRepository.那么我怎样才能获得所有团队呢?我现在的解决方案是使用两个存储库.

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?

我可以改为创建 TeamController 并编写一个返回所有团队的方法吗(我该怎么做)?

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

推荐答案

是的,控制器引用两个存储库是完全可以接受的.

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.

这篇关于最佳实践:在 MVC 控制器中使用多个存储库是否可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆