最好在MVC中拥有大型控制器或多个控制器? [英] Better to have huge Controllers, or many controllers, in MVC?

查看:154
本文介绍了最好在MVC中拥有大型控制器或多个控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在ASP.NET MVC中构建一个相当大的HR应用程序,到目前为止,我们的控制器变得越来越大.例如,我们有一个Employee控制器,并且包括了所有员工视图(个人信息,员工扣除额,家属等).这些视图中的每一个都可能具有多个操作或子视图(例如CRUD).每个动作都相对较小,但是控制器可能具有数十种功能.

We are building a fairly large HR application in ASP.NET MVC, and so far our controllers are becoming quite large. For example, we have an Employee controller, and all employee views are included (Personal info, employee deductions, dependents, etc). Each of these views might have multiple actions or subviews (e.g. CRUD). Each action is relatively small, but the controllers might have dozens of functions.

是否有拆分控制器的最佳实践?与其让Employee控制器具有数十个视图,不如为每个子类型(即EmployeePersonalInfoController,EmployeeDeductionController,EmployeeDependentController)配备一个控制器会更好吗?

Are there any best practices for splitting controllers? Instead of having an Employee controller with dozens of views, would it be better too have one controller for each subtype (i.e. EmployeePersonalInfoController, EmployeeDeductionController, EmployeeDependentController)?

最后,它甚至有关系吗?

And finally, does it even matter?

更新说明

我最初关心的是CRUD动作.例如,让我们考虑创建和删除...

My original concern was with CRUD actions. For example, let's consider Create and Delete ...

EmployeeController中的当前操作:

  CreateEmployee()
  DeleteEmployee()
  CreateEmployeeDeduction()
  DeleteEmployeeDeduction()
  CreateDependent()
  DeleteDependent()
  etc.

如果控制器拆分:

  EmployeeController
    Create()
    Delete()
  EmployeeDeductionController
    Create()
    Delete()
  EmployeeDependentController
    Create()
    Delete()
  EmployeeBenefitController
    Create()
    Delete()
  etc.

在第一种情况下,我们的〜100屏幕分成8-10个大型控制器.在第二个中,我可能会拥有约50个控制器.

In the 1st scenario, our ~100 screens get split into 8-10 large controllers. In the second, I'd probably have ~50 controllers.

推荐答案

在我的拙见中,如果您要使控制器中的代码保持关闭状态,那并不重要.

In my humble opinion, if you are keeping the code in your controllers down then it doesn't really matter.

您的大多数代码都将发生在某个地方的业务层中吗?如果是这种情况,那么您在控制器中真正要做的就是将数据返回到视图.应该的.

Most of your code would be happening in a business layer somewhere right? If that's the case then all you are really doing in your controller is returning data to the view. As it should be.

不确定我是否喜欢将控制器分成子类型.虽然您应该保持关注点的分离,但我认为子类型的作用有点过分了.

Not really sure if I'm a fan of seperating the controllers into subtypes. Whilst you should maintain seperation of concerns I think subtypes is going a little too far.

您可以看一下这篇文章,看看是否有帮助. 相同视图的不同路径

You could take a look at this post to see if it helps. Same View Different Paths

这可能比使用您建议的子类型方法更好.

That may be a better solution than using a subtype approach that you suggested.

这篇关于最好在MVC中拥有大型控制器或多个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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