AutoMapper-展平/取消展平至列表

问题描述 投票:2回答:2

我想将我的ASP.NET Core REST Web服务的实体框架模型数据展平到dto。

我的实体类(简化)如下:

public class DeliveryNoteEntity
{
    public string VehicleNo { get; set; }
    public int NotMapped { get; set; }
    public List<DeliveryNoteSignature> Signatures { get; set; }
}

public class DeliveryNoteSignature
{
    public string Signature { get; set; }
    public SignedByRole SignedBy { get; set; }
}

public enum SignedByRole
{
    Driver = 1,
    Recipient = 2
}

我的dto看起来像

public class DeliveryNoteDto
{
    public int Id { get; set; }
    public string VehicleNo { get; set; }
    public string DriverSignature { get; set; }
    public string RecipientSignature { get; set; }
}

然后我可以这样填写我的实体

var sourceEntity = new DeliveryNoteEntity
{
    VehicleNo = "VehicleNo20",
    Signatures = new List<DeliveryNoteSignature> { new DeliveryNoteSignature { Signature = "Driver Mr. Pitz", SignedBy = SignedByRole.Driver} }
};

并映射到dto:

Mapper.Initialize(cfg => cfg.CreateMap<DeliveryNoteEntity, DeliveryNoteDto>()
                                    .ForMember(dest => dest.DriverSignature, opt => opt.MapFrom(src => src.Signatures
                                                                                                          .Where(x => x.SignedBy == SignedByRole.Driver)
                                                                                                          .Select(x => x.Signature)
                                                                                                          .FirstOrDefault()))
                                    .ForMember(dest => dest.RecipientSignature, opt => opt.MapFrom(src => src.Signatures
                                                                                                             .Where(x => x.SignedBy == SignedByRole.Recipient)
                                                                                                             .Select(x => x.Signature)
                                                                                                             .FirstOrDefault()))
                                    .ReverseMap()
                                    );
var dto = Mapper.Map<DeliveryNoteDto>(sourceEntity);

所以我的问题是:当返回到我的服务时,如何从dto到实体进行反向映射?因此,我将手动执行以下操作:

var entityToSave = new DeliveryNoteEntity()
{
    VehicleNo = dto.VehicleNo,
    Signatures = new List<DeliveryNoteSignature>
    {
         new DeliveryNoteSignature {SignedBy = SignedByRole.Driver, Signature = dto.DriverSignature},
         new DeliveryNoteSignature {SignedBy = SignedByRole.Recipient, Signature = dto.RecipientSignature}
    }
};

可以通过AutoMapper完成吗?

EDIT:我的真正问题是如何从dto中的XXXSignature属性映射到实体中的列表?

c# asp.net-core automapper
2个回答
0
投票

如果使用AutoMapper的命名约定,那么ReverseMap也将为您完成。

class CustomerClass
{
    public string Name {get; set;}
    public string Surname {get;set;}
}

class MainClass
{
     public CustomerClass Customer {get; set;}
}

class MainClassDto
{
    public string CustomerName {get; set;}
    public string CustomerSurname {get; set;}

}

对于此示例,您无需为映射和/或反向映射进行任何配置。 AutoMapper使用其默认配置进行处理


0
投票

我最终通过代码完成了此操作,因此我可以清楚地检测并定义添加,删除和更新操作。因此,我仅使用AutoMapper的一种方式:实体-> dto。

© www.soinside.com 2019 - 2024. All rights reserved.