MVC:存储库和视图模型都一起构建以获得更好的结构?
如果我想结合使用每个实体的存储库和每个视图的视图模型,效果如何?
我可以查找任何网站提示吗?也许有人可以举一个简单的例子?
谢谢
最诚挚的问候!
If I want to combine using repositorys per entity and Viewmodels per view how does it work out?
Any website tips I could look up? Maby someone could give an easy example?
Thanks
Best Regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我喜欢以下结构(来自著名的 Steven Sanderson 的 Pro ASP.NET MVC 系列):
域项目(业务逻辑):
Web UI 项目(MVC Web 应用程序):
最主要的是您将业务逻辑(应容纳您的存储库)与 Web 分离UI(MVC 项目)
在这种情况下,您的控制器类引用域层并使用 DI/IoC 来调用存储库的正确实例。
控制器类示例:
I like the following structure (from the famous Steven Sanderson's Pro ASP.NET MVC series):
Domain Project (Business Logic):
Web UI Project (MVC Web App):
The main thing is you're separating your business logic (which should house your repositories) from your Web UI (the MVC project)
In this scenario, your Controller classes reference the domain layer and use DI/IoC to call up the correct instance of the repository.
Example controller class:
使用 AutoMapper 将数据从实体复制到模型,反之亦然。
这将减少您必须编写的大量“管道”代码。
Use AutoMapper to copy data from entities to models and vice-versa.
This will reduce a lot of 'plumbing' code you will have to write otherwise.
我不是专业的开发人员,但我认为 Steve Sanderson 的模型对于某些项目来说不是正确的模型,因为你直接根据模型的观点进行工作。如果您只想显示少数属性而不是全部属性,会发生什么情况?您的完整模型正在前往视图。
我认为你的视图必须针对 viewmodel 类,而不是直接来自 orm 的模型(通过存储库等)。
我发现的唯一问题是模型到视图模型以及视图模型到模型之间的映射过程。让我解释一下...
我试图用 automap 进行此映射,模型 -> 之间的方向viewmodel 工作正常,但在另一个方向(viewmodel 到模型)我找不到自动执行此操作的方法,因为 viewmodel 通常不拥有模型所具有的所有属性,并且如果您经常自动映射到模型对象属性为空。
最后,您始终需要进行一些手动映射。
针对这种情况的想法可能会受到欢迎。
谢谢
I'm not a professional developer but I think Steve Sanderson's model is not the right model for some projects because you are working in your views against the model directly. What happen if you want to show only a few properties and not all of them? Your full model is traveling to the view.
I think your views must work against viewmodel classes and not directly the model coming from orm (trough repository, etc.)
The only problem that I'm finding is the mapping process between model to viewmodel and viewmodel to model. Let me explain...
I was trying to do this mapping with automap, the direction between model -> viewmodel works fine, but in the other direction (viewmodel to model) I'm not finding the way to do it automatically because the viewmodel, normally does not own all the properties that model have and if you do an automap to model object a lot of properties are empty.
Finally you need to make always some manual mappings.
Ideas for this situation may be welcome.
Thanks