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

后端 未结 7 1199
-上瘾入骨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:12

    Controllers are meant to be containers for actions under one context. I.E. a customer controller would have actions pertaining to controlling customers. This is particularly suited to CRUD. I would go with fewer larger controllers for this reason. That said, it is really up to you as the application architect to choose the way that best suits your code and just because it is more common to do it one way doesn't mean you have to.

    If you have large amounts of code I would suggest you look into ASP.NET MVC areas. You can find excellent posts about it Here in Scott Gu's blog and Here in Steve Sanderson's blog. If you have so many controllers, it might be suitable for you.

    Just had a thought after re-reading your post, I suspect your example doesn't come close to the level of complication you have in your code. Perhaps it might help if you posted a situation where you were unsure whether or not it was a good idea to split your controller that is more specific (and less CRUDDY, because CRUD is fairly straight forward).

提交回复
热议问题