asp mvc design question: single view for multiple data types
In my database, there are maybe a dozen tables that represent lookup types (All have ID and Name columns - No, they must remain as seperate tables, won't be normalized into one table). I'm building an administration application, where I want an administrator to be able to add/edit/delete/list all of these lookups. I know I can re-use the model, but is there a more efficient way for me to write this in an asp mvc 2 application, oth开发者_JAVA技巧er than writing 3 views, 1 controller for each lookup type resulting in 48 views and 12 controllers?
I wouldn't recommend removing the coupling between your models, the controllers, and views. The idea behind this pattern is that you get to separate the business logic from the presentation from the workflow. If you start mixing multiple models into the same views/controllers, it gets messy.
Maybe try doing what you're attempting using a master page and having your models inherit from a common base class or something like that instead? This way you can reuse code without smashing it into the same files.
What about a more advanced ORM mapping that combines all of them in to a single entity?
I would do this with one controller and an action/view for each CRUD operation (1x4). When and if the schema changes on one or more lookup tables, I would deal with it then. Otherwise, think of the work entailed if you add a column to each of the tables: you would have to update 48x12 or whatever files.
The specifics of how you do it depend on your model, but it could be done simply using ADO.NET and returning a DataTable or similar populated with a dynamic SQL statement where the table name is supplied by the view. You would need to guard against SQL injection, of course, if you were using string concatenation.
The Create view would similarily use the table name coming from the posted form to know which table to update with the posted data.
Look into using T4 templates (decent example) to build everything out. You should be able to code gen the controllers and views based each model.
Sorry, my answer is to go with the 48 views and 12 controllers ... have the code gen set up so that if you add more lookup types, all you need to do is re-run the code, and boom, controllers and views are set up.
精彩评论