AutoMapper-讨人喜欢/不讨人喜欢的列表

时间:2019-02-06 13:23:30

标签: c# asp.net-core automapper

我想将我的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吗?

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

2 个答案:

答案 0 :(得分: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使用其默认配置进行处理

答案 1 :(得分:0)

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