如何将 DTO 从 EF 映射到模型
我的 UI MVC 层中有以下模型 Person:
public class Person
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public int PersonTypeID { get; set; }
[Required]
public string Phone { get; set; }
[Required]
public string Email { get; set; }
}
在我的数据层中,我有一个具有相同属性名称但不同元(自然)的类:
public partial class Person : EntityObject { ... }
如何将数据从我的数据层返回到我的 MVC UI 层而不需要数据层了解MVC UI层吗?
注意:我还有一个简单的 IPerson 接口,具有相同的属性名称。
I have the following model Person in my UI MVC layer:
public class Person
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public int PersonTypeID { get; set; }
[Required]
public string Phone { get; set; }
[Required]
public string Email { get; set; }
}
In my data layer, I have a class with the same property names, but different meta (naturally):
public partial class Person : EntityObject { ... }
How can I return data from my data layer into my MVC UI layer without having the data layer know about the MVC UI layer?
Note: I also have a simple IPerson interface with the same property names as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 AutoMapper 在域模型和视图模型之间进行映射。 MVC层知道数据层,但数据层不需要知道MVC层。
这是一个常见的模式:
反之亦然:
如您所见,数据层不需要了解有关 MVC 层的任何信息。 MVC 层需要了解数据层。
You could use AutoMapper to map between the domain model and the view model. It is the MVC layer that knows about the data layer, but the data layer doesn't need to know about the MVC layer.
Here's a common pattern:
and the other way around:
As you can see the data layer doesn't need to know anything about the MVC layer. It's the MVC layer that needs to know about the data layer.
我邀请您查看一个框架,例如 automapper,它使您能够轻松执行对象到对象映射。
I would invite you to review a framework such automapper which gives you the ability to easily perform object to object mapping.
其他选项是 CX.Mapper 使用起来非常简单,您不需要预先配置地图:
您可以使用 NuGet 安装
Other option is CX.Mapper very simple to use and you dont need to precofig the Map:
You can install with NuGet