MVC 将视图模型映射到 POCO
我正在使用 EF 4 和 POCO 开发 ASP.NET MVC 3 应用程序。我面临的主要挑战之一是如何将 ViewModel 映射到 POCO。
例如:
我有两个 POCO 对象 ->用户和订单。此外,我有一个 ViewModel --> UserOrderModel 包含两个模型。每当发布一些数据时,我想将 UserOrderModel 映射到 User 和 Order 对象。执行此操作非常简单(也许只是根据控制器中的请求进行映射),但我想知道是否有一个很好的、干净的解决方案来解决该问题(我最初考虑为所有视图模型创建一个通用映射接口,以便它们必须定义如何将自己映射到 POCO 对象)
提前致谢
I am developing an ASP.NET MVC 3 application using EF 4 and POCOs. One of the main challenges that I have is how to map a ViewModel to a POCO.
For example:
I have two POCO object -> User and Order. Furthemore I have a ViewModel --> UserOrderModel containing both models. Whenever some data is posted I would like to map the UserOrderModel to a User and an Order object. It is pretty straightforward to do this (perhaps just map on request in the controller) but I am wondering if there is a nice, clean solution for that problem (I was initially thinking of creating a generic mapping interace for all view models so that they must define how to map themselves to a POCO object)
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来像是 AutoMapper 的工作,
很好的例子 此处介绍映射 MVC ViewModel 到 POCO 的。
Sounds like a job for AutoMapper
Great example here on mapping MVC ViewModel's to POCO's.