MVC 将视图模型映射到 POCO

发布于 2024-10-05 13:00:33 字数 340 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨凡馨 2024-10-12 13:00:33

听起来像是 AutoMapper 的工作,

很好的例子 此处介绍映射 MVC ViewModel 到 POCO 的。

Sounds like a job for AutoMapper

Great example here on mapping MVC ViewModel's to POCO's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文