I am working on MVC project and on Admin side I have to crea开发者_JAVA技巧te CRUD forms for Products
, Category
, SubCategory
.
Which of these approach will be better:
- Create one
AdminController
and have CRUD Action methods for Products, Category, SubCategory. - Create Separate Controllers for Products, Category, SubCategory which have individual CRUD Action Methods?
Thanks for help
As the others have said, is better to have separate Controllers. I would alse recommend to put them in an Admin Area so you have the functionality "separated" from the main site.
It's always best to keep your controllers as light as possible so I'd go for separate controllers for each. You might want to take a look at the MVC Controller Scaffolding feature which is now more easily available in the MVC 3 Tools Update. It's perfect for this kind of "basic" CRUD work.
i would opt for having every functionality with Products in the products controller etc. mainly because of the Single responsibility principle
in MVC3 you can automatically generate controllers with CRUD methods / screens if you use the add controller wizard.
example: http://msdn.microsoft.com/en-us/data/gg685467
imo the controller should only be a thin layer talking to a business service layer who's handling all the business logic. For example the products you could create a ProductService that will handle your business logic.
Usually, it's a good thing to follow REST principles.
The idea is quite simple - every "resource" should map to controller (approach #2).
However, I think it's better to "cut along natural joints".
With that I mean - sometimes decomposition (dividing into more controllers) just for sake of it leads to unnecessary complexity. Controllers should appear accordingly to Your use cases.
Start with one (which appears as most important) and slice next when current one exceeds ~100 loc.
精彩评论