在 MVC 中拥有巨大的控制器或许多控制器更好吗? [英] Better to have huge Controllers, or many controllers, in MVC?

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

问题描述

我们正在 ASP.NET MVC 中构建一个相当大的 HR 应用程序,到目前为止我们的控制器变得相当大.例如,我们有一个员工控制器,包括所有员工视图(个人信息、员工扣减、家属等).这些视图中的每一个都可能有多个操作或子视图(例如 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)?

最后,这还重要吗?

更新说明

我最初关心的是 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天全站免登陆