MVC 和单独的项目
我是 MVC 新手。我希望将数据对象放在与主 MVC 项目不同的单独项目中。我是否只需将“models”文件夹中的所有类和文件移动到新的 all 项目,然后在 MVC 项目中添加对新数据项目的引用?然后,我将如何在我的控制器(仍在 MVC 项目中)以及我的视图中使用这些模型?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将使用适合您的实施技术的工具来创建库。例如,使用 Java,您可以创建包含模型的 jar。您可以将 jar 放在使用它们的项目的类路径中;一旦你这样做了,你就可以访问图书馆了。
如果您想在多个地方重用它们实现的模型对象和业务需求,这是一个好(很棒)的想法。例如,也许您希望客户使用您的模型 API 来实现他们自己的客户端类型。
您可以将模型层作为其自己的项目进行维护,或者将其保留在“主”项目中并通过构建流程来生成所需的工件。您没有向我们提供足够的信息,无法就最佳方法是什么给出明确的答案。
我可以这样说:大多数项目都是从一个项目中的所有层开始的。如果项目的一部分变得非常复杂,您可以重构单独的项目——在我见过的一些项目中,Web 客户端足够复杂,可以成为拥有自己的开发团队(以及自己的 MVC 层)的自己的项目。在其他项目中,整个 MVC 堆栈都在一个项目中。请注意,您不需要 MVC 中的 M 位于单独的项目中即可遵循该范例。您只需要在正确的层中处理所有问题即可。
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.
你正朝着正确的方向前进。事实上,对于除了最琐碎的项目之外的所有项目,我喜欢将我的模型放在单独的类库中。然后,正如您所提到的,您可以从 MVC 项目引用类库并使用模型类。这样做的好处是,一旦您在类库中准备好模型并进行完善,您就可以“公开”到任何类型的表示层,例如 MVC 项目或 Windows Phone 7 或通过 WCF 公开。
请记住,存储库不是您模型的一部分。它们是持久性问题,应该只执行 CRUD 操作,简单明了。任何类型的操作都应该在模型层进行。
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.