问题
How to organize models in multiple areas ? I am trying to organize code using areas. I am using visual studio 2010 express , the project is on asp.net mvc3 and code-first development is used. All the tutorial and reference over internet only talks about controllers and routes, but what about models ? more precisely, I am looking for suggestions around :
- The model folder in an area will contain its own classes, so where should the dbcontext be placed ? I already have an dbcontext class containing multiple dbset entries in the "root" model folder. Can i have multiple dbcontext storing its data in the same database ?
- I have gone through asp.net mvc portable areas project by mvccontrib, but its still in asp.net mvc2, but is that the only way around for building plugins/components in asp.net mvc 3 ?
- Apart from areas/portable areas, is there any better way to organize code is such a way that new features can be simply "dropped-in", without doing too much changes in existing code ? Areas looks promising but i am not able to figure out how models should be placed.
- Are areas only good for controller and view separation ?
回答1:
The dbcontext will be in data access layer or your data access folder where you will manage all your data access files. You will reference these classes in your models wherever you want to use them. Yes! You can have multiple dbcontext storing its data in the same db.
No Idea.
The best way I or my team does is to maintain views/models/controllers totally separate. In that way if one person is dedicated to the views or how to show them, other can write controller actions for it.
We dont use areas as because when your application tend to scale in a exponential manner, you might will loose hold of what is where? My mantra: 4 folders: View/Model/ViewModel/Controller.
Any other person might have different view...
回答2:
I've been looking into possible ways to achieve the model level separation of DbContext, so far what i found is that there is no direct way to have multiple dbcontext storing data in the same database with Code-First. It is only possible with Model-First design. With code-first multiple dbinitializers doesn't works as all try to initialize the database and only the first one will succeed. However, there might be some way around by dynamically creating the database using DbModelBuilder as done in orchard CMS and some other Open source projects. Anybody tried it ? Please share ...
回答3:
- Areas are only good for view model separation.
The registration code of an area is a good hint towards what areas are used for - separating out routes and access to specific controllers. (Think logical user interface units). For organising your code, namespaces are the tool of choice.
Perhaps look at something like Umbraco, Orchard or even N2CMS to see how they manage extensions.
来源:https://stackoverflow.com/questions/9071342/models-in-asp-net-mvc-3-areas