gpt4 book ai didi

c# - 从 ViewModel 映射回 DTO 时出现 AutoMapper 问题

转载 作者:太空狗 更新时间:2023-10-29 21:58:13 31 4
gpt4 key购买 nike

我在映射回 DTO 时遇到问题。

DTO:

public class ServiceEntity : BaseChild
{
public int Id { get; set; }
}

public class BaseChild
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Salary { get; set; }
public string BkName { get; set; }
public int BkPrice { get; set; }
public string BkDescription { get; set; }
}

View 模型:

public class BusinessEntity 
{
public ChildBussiness Details { get; set; }
}

public class ChildBussiness
{
public string NameFirst { get; set; }
public string LastName { get; set; }
public Books BookDetails { get; set; }
public string Salary { get; set; }
}

public class Books
{
public string BookName { get; set; }
public int BookPrice { get; set; }
public string BookDescription { get; set; }
}

Controller

对于从 DTO 到 ViewModel 的映射,我使用以下代码,它工作正常。

public ActionResult Index()
{
ServiceEntity obj = GetData();
Mapper.CreateMap<ServiceEntity, BusinessEntity>()
.ForMember(d => d.Details, o => o.MapFrom(x => new ChildBussiness { NameFirst = x.FirstName, LastName = x.LastName, Salary = x.Salary.ToString(), BookDetails = new Books { BookDescription = x.BkDescription, BookName = x.BkName, BookPrice = x.BkPrice }}));

BusinessEntity objDetails = Mapper.Map<ServiceEntity, BusinessEntity>(obj);
}

虽然转换回来我做不到。下面是我试过的代码。

.
.
.
ServiceEntity objser = new ServiceEntity();

Mapper.CreateMap<BusinessEntity, ServiceEntity>();
Mapper.CreateMap<Books, ServiceEntity>();

objser = Mapper.Map<BusinessEntity, ServiceEntity>(model);
.
.
.

但我没有取得任何成功。例如,我提供了一些属性。我实时我可能拥有超过 30 个属性(property)。任何建议将不胜感激...

最佳答案

编辑 因此,您可以更改 BusinessEntity 实现,您可以使用 AutoMapper 约定和扁平化的强大功能。这是修改后的业务实体:

public class BusinessEntity
{
public string FirstName { get; set; } // Instead of NameFirst
public string LastName { get; set; }
public Book Bk { get; set; } // Bk instead of BookDetails
public string Salary { get; set; }
}

public class Book
{
public string Name { get; set; } // No prefixes
public int Price { get; set; }
public string Description { get; set; }
}

您需要 Bk 名称以允许将 Bk.Name 扁平化为 BkName。现在所有映射都将在几行中生成:

// For mapping from service entity to book
Mapper.Initialize(cfg => cfg.RecognizePrefixes("Bk"));

Mapper.CreateMap<BusinessEntity, ServiceEntity>();

// Trick to un-flatten service entity
// It is mapped both to Book and BusinessEnity
Mapper.CreateMap<ServiceEntity, Book>();
Mapper.CreateMap<ServiceEntity, BusinessEntity>()
.ForMember(d => d.Bk, m => m.MapFrom(s => s));

就是这样。所有 30 个属性都将按照约定进行映射:

var service = new ServiceEntity {
FirstName = "Sergey",
LastName = "Berezovskiy",
Salary = 5000,
BkName = "Laziness in Action",
BkDescription = "...",
BkPrice = 42
};

var business = Mapper.Map<BusinessEntity>(source);
var anotherService = Mapper.Map<ServiceEntity>(business);

原始答案 因此您正在使用自定义映射,从服务实体到业务实体,那么默认映射也不适用于向后映射。您应该手动为成员提供映射:

Mapper.CreateMap<BusinessEntity, ServiceEntity>()
.ForMember(d => d.FirstName, m => m.MapFrom(s => s.Details.NameFirst))
.ForMember(d => d.LastName, m => m.MapFrom(s => s.Details.LastName))
.ForMember(d => d.Salary, m => m.MapFrom(s => s.Details.Salary))
.ForMember(d => d.BkName, m => m.MapFrom(s => s.Details.BookDetails.BookName))
.ForMember(d => d.BkPrice, m => m.MapFrom(s => s.Details.BookDetails.BookPrice))
.ForMember(d => d.BkDescription, m => m.MapFrom(s => s.Details.BookDetails.BookDescription));

但我认为在这种情况下手动映射更好,然后为各个属性提供所有这些映射。

关于c# - 从 ViewModel 映射回 DTO 时出现 AutoMapper 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993383/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com