ASP.NET MVC:将 DTO/EditModel 放入数据库的最佳实践或设计模式
我正在使用 AutoMapper 为 ViewModel 和 EditModel 创建 DTO。我找不到关于如何获取 EditModel 并将其包含的更改应用到数据库的好示例。
我首先考虑使用 AutoMapper 作为双向机制,但显然不推荐: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way -mapping-in-automapper.aspx
那么,从控制器获取 DTO 或 EditModel 到数据存储的最佳方法(设计模式)是什么?
更新:基于一些对于答案,我应该提到我确实使用了存储库设计模式和实体框架。
更新:我的问题太主观了,因为没有通用的做法可以做到这一点。我的结论是,很多人都以两种方式使用 AutoMapper,即使其他人不推荐它。
I am using AutoMapper to create DTOs for the ViewModel and EditModel. I can't find out a good example of how to take the EditModel and apply the changes it contains to the database.
I first considered using AutoMapper as a two-way mechanism, but it's obviously not recommended: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx
So, what's the best way (design pattern) for getting a DTO or an EditModel from the Controller to the data store?
UPDATE: based on some of the answers, I should mention that I do use the Repository design pattern and the Entity Framework.
UPDATE: My question is far too subjective, as there isn't a general practice to do this. My conclusion is that a lot of people use AutoMapper both ways even if others don't recommend it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在最简单的形式中,您只需使用 AutoMapper 将模型映射到 Domain/DTO 对象,然后将其持久保存到数据存储中,无论您如何执行此操作。
所以,这几乎是我做过的项目之一:
您可能希望从您调用的任何方法中返回 id,以便您可以进一步使用它。这只是一个粗略的例子,但这就是它的基础...
编辑:
Mapper.Map
行将 ViewModel 数据放入 Domain 对象中。从那里,Save()
调用您需要的任何内容。在您的情况下,这将是您的IRepository.Save()
方法。对我来说,这会调用 nHibernate 的 SaveOrUpdate在我的 AutoMapper 配置中,我有一些类似的东西:
?
这有帮助吗
In it's simplest form, you just need to use AutoMapper to map your model onto your Domain/DTO object, then persist that to your datastore however you do that.
So something that's pretty much out of one of the projects I've done:
You might want to return the id out of whatever method you're calling this so you can use it further. It's just a rough example, but that's the basics of it...
EDIT:
The line that is:
Mapper.Map
puts the ViewModel data into the Domain object. From there, theSave()
calls whatever you need. In your case, it would be yourIRepository.Save()
method. For me, that calls into nHibernate'sSaveOrUpdate
In my AutoMapper configuration, I have some thing like:
and
Does that help?
您的问题可能有很多答案,此处无法一一列出。也就是说,我认为您接下来要研究的东西称为 ORM 或对象关系映射器。一些更常见的选项是 NHibernate 和实体框架。这将允许您获取应用程序中的对象并将它们映射到数据库。
AutoMapper 是一个很棒的工具 - 但我相信其目的是在对象(如视图模型和实体)之间进行映射。
编辑:并感觉到您的问题涉及设计模式,我建议您研究存储库模式 - 它与 ASP.NET MVC 配合良好,并且有很多示例。
There are going to be alot of possible answers to your question, too many to list here. That said - I think the thing you want to research next is called an ORM or Object Relational Mapper. Some of the more common options are NHibernate and Entity Framework. This will allow you to take objects in your application and map them to your database.
AutoMapper is a great tool - but I believe the intent is to map between objects (like a view model and an entity).
EDIT: And sense your questions asks about design patterns, I would suggest looking into the Repository pattern - it works well with ASP.NET MVC and there are numerous examples out there.
我使用 AutoMapper 进行两个方向的操作。您只需要确保在映射回时自己处理所有集合即可。 AutoMapper 可以很好地展平结构,但不能很好地重构结构。
I use AutoMapper to go both directions. You just need to be sure that you handle any collections yourself when mapping back. AutoMapper works great to flatten a structure but not so well to reconstitute it.
NHibernate 是将 DTO 映射到数据库表的最佳方式。实体框架还没有准备好迎接黄金时期,就像 ASP.NET MVC 一样。使用 NHibernate 的 Web 表单是最好的方法。
NHibernate is the best way to map your DTO's to your database tables. Entity Framework is not ready for prime time yet, just like ASP.NET MVC. Web Forms with NHibernate is the best way to go.