MVC 应用程序中的控制器应该是每个域对象 1 个还是每组相关操作 1 个?
我有 MVC 应用程序,当我创建时,我从域模型 Person 的一个控制器开始。我的操作包括 AddPerson、DeletePerson、EditPerson 等。然后我开始添加更多操作,例如 AddPersonBoss 和 RemovePersonBoss。很快我就可以在一个控制器中执行 20 个操作。 MVC 的好处变得不那么明显,因为我的文件包含数百行代码并且很难理解。
我看到的所有示例都显示每个域对象(例如人员)一个控制器。感觉我应该有一个用于 AddPerson、DeletePerson、EditPerson 的 PersonManagement 控制器和一个用于 AddPersonBoss、RemovePersonBoss 的 BossManagement 控制器。
这有道理吗?为什么我不想将其分成多个控制器?一个控制器中的操作太多会变得很困难,但我看到的所有示例都是这样做的。
I have MVC app and when I create I start with one controller for my domain model Person. I have actions including AddPerson, DeletePerson, EditPerson, etc. Then I start adding more like AddPersonBoss and RemovePersonBoss. Pretty soon I have 20 actions in one controller. The benefit of MVC becomes less apparent because my file contains several hundred lines of code and is difficult to follow.
All examples I see show one controller per domain object such as Person. It feel like I should have a PersonManagement controller for AddPerson, DeletePerson, EditPerson and a BossManagement controller for AddPersonBoss, RemovePersonBoss.
Does this make sense? Why would I not want to break it into several controllers? Too many action in one controller becomes difficult, but all examples I see do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议同时使用这两种方法,因为有时仅使用一种特定方法有点困难。如果控制器显着增长,则考虑将其拆分为多个部分类。
I would suggest to use both approaches, because sometimes it is a bit difficult to use only one particular approach. If controller is growing significanly then consider to split it into multiple partial classes.
这取决于您的应用程序,
我更喜欢为每个操作创建一个控制器,因为这使我的控制器代码变得简单,
但如果您有很多操作,这将导致为每个
控制器创建很多文件,那么您必须创建一个文件将一些控制器组合在一起以使
应用程序更简单
that is depend on your application
i prefer to make a controller for each operation because that makes my controller code simple
but if you have a lot of operations that will lead to creating a lot of files for each
controller so then you have to make one file to group some controllers together to make the
application simpler