如何将 DTO 从 EF 映射到模型

发布于 2024-12-24 02:59:27 字数 568 浏览 5 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(3

朮生 2024-12-31 02:59:27

您可以使用 AutoMapper 在域模型和视图模型之间进行映射。 MVC层知道数据层,但数据层不需要知道MVC层。

这是一个常见的模式:

public ActionResult Foo()
{
    var person = _repository.GetPerson();
    var personViewModel = Mapper.Map<Person, PersonViewModel>(person);
    return View(personViewModel);
}

反之亦然:

[HttpPost]
public ActionResult Foo(PersonViewModel personViewModel)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    var person = Mapper.Map<PersonViewModel, Person>(personViewModel);
    _repository.UpdatePerson(person);
    return RedirectToAction("Success");
}

如您所见,数据层不需要了解有关 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:

public ActionResult Foo()
{
    var person = _repository.GetPerson();
    var personViewModel = Mapper.Map<Person, PersonViewModel>(person);
    return View(personViewModel);
}

and the other way around:

[HttpPost]
public ActionResult Foo(PersonViewModel personViewModel)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    var person = Mapper.Map<PersonViewModel, Person>(personViewModel);
    _repository.UpdatePerson(person);
    return RedirectToAction("Success");
}

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.

初与友歌 2024-12-31 02:59:27

我邀请您查看一个框架,例如 automapper,它使您能够轻松执行对象到对象映射。

I would invite you to review a framework such automapper which gives you the ability to easily perform object to object mapping.

糖果控 2024-12-31 02:59:27

其他选项是 CX.Mapper 使用起来非常简单,您不需要预先配置地图:

[HttpGet]
public ActionResult Edit(int id)
{
  var item = this.db.Items.Find(id);
  var model = CX.Mapper.Mapping.MapNew<Item, ItemDto>(item);
  return View(model);
}

[HttpPost]
public ActionResult Edit(ItemDto model)
{
  if(Model.IsValid)
  {
    var item = this.db.Items.Find(ItemDto.Id);
    CX.Mapper.Mapping.Map<ItemDto, Item>(model, item);
    this.db.SaveChanges();
    return RedirectToAction("Index");
  }
  return View(model);
}

您可以使用 NuGet 安装

Other option is CX.Mapper very simple to use and you dont need to precofig the Map:

[HttpGet]
public ActionResult Edit(int id)
{
  var item = this.db.Items.Find(id);
  var model = CX.Mapper.Mapping.MapNew<Item, ItemDto>(item);
  return View(model);
}

[HttpPost]
public ActionResult Edit(ItemDto model)
{
  if(Model.IsValid)
  {
    var item = this.db.Items.Find(ItemDto.Id);
    CX.Mapper.Mapping.Map<ItemDto, Item>(model, item);
    this.db.SaveChanges();
    return RedirectToAction("Index");
  }
  return View(model);
}

You can install with NuGet

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