问题
I am new to MVC. I'd like to have my data objects in a separate project from my main MVC project. Would I just move ALL the classes and files from my 'models' folder to my new all project, and then add a reference to the new data project in my MVC project? How would I then make used of the models, both in my controllers (still in the MVC project) as well as within my views?
回答1:
You are going in the right direction. In fact, for all but the most trivial projects, I like to have my model in a separate class library. And then, as you mentioned, you can reference the class library from your MVC project and use your model classes. The benefit being once you have your model ready and polished in a class library, you can "expose" to any kind presentation layer such as your MVC project or Windows Phone 7 or expose through WCF.
Keep in mind though that repositories are NOT part of your model. They are a persistence concern and they should only do the CRUD operations, plain and simple. Any kind of manipulation should go in the models layer.
回答2:
You would create libraries with the tools appropriate to your implementation technology. For example, with Java you would create jar(s) containing the models. You would put the jars on the classpath of whichever project(s) used them; once you do that you have access to the library.
This is a good (great) idea if you want to reuse the model objects and business requirements they implement in more than one place. Perhaps you want customers to use your model API to implement their own sorts of clients, for example.
You could maintain the model layer as its own project, or keep it in the 'main' project and have build processes to produce the desired artifacts. You haven't given us enough information to give a hard answer on what the best approach is.
I can say this: most projects start with all the layers within the one project. If a part of the project becomes significantly complex, you can refactor out separate projects--On some projects I have seen, the web client is sufficiently complex to be its own project with its own development team (and its own MVC layers). On other projects, the entire MVC stacks were all in one project. Note that you do not need the M in MVC to be in a separate project to follow the paradigm. You just need to handle all the concerns in the correct layer.
来源:https://stackoverflow.com/questions/4581569/mvc-and-separate-projects