Better to have huge Controllers, or many controllers, in MVC?

后端 未结 7 1186
-上瘾入骨i
-上瘾入骨i 2021-01-30 01:23

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

7条回答
  •  鱼传尺愫
    2021-01-30 02:04

    Another approach we've been using is having a ControllerBase to keep cross-cutting concerns in a common place for CRUD operations. This controller declares the common operations and includes extension points for the specific entity stuff. We had too many code duplication without something like this.

    Then, you inherit this controller and create one per entity. And yes, there are many controllers but having so many screens, I don't think it will be the main problem.

    Most of the actions accept a complex Model and we play then with the model binders to remove clutter from the controllers. You can see a good post about that here.

    Then, using areas like @Odd suggests is a good idea, at least to separate the views because when you have a lot of them is a mess.

    Hopefully ASP.NET MVC v2 will bring us areas and encapsulating views in different assemblies (actually that can be done now extending the VirtualPathProvider class).

提交回复
热议问题