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.
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?
Updated Clarification
My original concern was with CRUD actions. For example, let's consider Create and Delete ...
Current Actions in EmployeeController:
CreateEmployee() DeleteEmployee() CreateEmployeeDeduction() DeleteEmployeeDeduction() CreateDependent() DeleteDependent() etc.
If the controllers were split:
EmployeeController Create() Delete() EmployeeDeductionController Create() Delete() EmployeeDependentController Create() Delete() EmployeeBenefitController Create() Delete() etc.
In the 1st scenario, our ~100 screens get split into 8-10 large controllers. In the second, I'd probably have ~50 controllers.
Understanding Views. The two controller actions exposed by the HomeController class, Index() and About(), both return a view. A view contains the HTML markup and content that is sent to the browser. A view is the equivalent of a page when working with an ASP.NET MVC application.
Yes we can create multiple controllers in Spring MVC.
Do I need a controller for each model? No, not necessarily. However, having one controller per RESTful resource is a convention for a reason, and you should carefully analyze why that convention isn't meeting your needs before doing something completely different.
Partial classes allow you to spread your class across multiple files. That way you can group relevant areas of your controller into separate files, and yet they'll all still be part of the same controller. e.g.
EmployeeDeductionController.cs
public partial class EmployeeController { public ActionResult Deduct() { } // etc }
EmployeeBenefitController.cs
public partial class EmployeeController { public ActionResult GiveBenefit() { } // etc }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With