How to reverse map to an existing parent object using AutoMapper?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 09:13:32

问题


Consider the following classes:

public class Customer
{
    public Guid Id { get; set; }
    public List<Address> Addresses { get; set; }
    public List<Order> Orders { get; set; }
}

public class Address
{
    public Guid Id { get; set; }
    public Customer Customer { get; set; }    // parent
    // ...
}

public class Order
{
    public Guid Id { get; set; }
    public Customer Customer { get; set; }    // parent
    public Address Address { get; set; }
    // ...
}

And the data transfer objects (DTOs):

public class CustomerDto
{
    public Guid Id { get; set; }
    public AddressDto[] Addresses { get; set; }
    public OrderDto[] Orders { get; set; }
}

public class AddressDto
{
    public Guid Id { get; set; }
    public Guid CustomerId { get; set; }
    // ...
}

public class OrderDto
{
    public Guid Id { get; set; }
    public Guid CustomerId { get; set; }
    public Guid AddressId { get; set; }
}

Mapping (and flattening) from Customer to CustomerDto is easy. However, I'm having trouble reverse mapping from CustomerDto to Customer.

AutoMapper is not able to figure out the Customer property in Address or Order. It also can't figure out the Address property in Order.

I can make it work by first "ignoring" these properties by using the Ignore configuration method, and then using AfterMap to manually find the objects and assign them appropriately.

Is there a way for AutoMapper to do this automatically?


回答1:


I think what you are looking for is .ReverseMap() - http://docs.automapper.org/en/stable/Reverse-Mapping-and-Unflattening.html

So given the following mappings:

        var mapper = new MapperConfiguration(expression =>
        {
            expression.CreateMap<Customer, CustomerDto>()
                .ForMember(dto => dto.Id, obj => obj.MapFrom(customer => customer.Id))
                .ReverseMap();
            expression.CreateMap<Address, AddressDto>()
                .ForMember(dto => dto.Id, obj => obj.MapFrom(address => address.Id))
                .ForMember(dto => dto.CustomerId, obj => obj.MapFrom(address => address.Customer.Id))
                .ReverseMap();
            expression.CreateMap<Order, OrderDto>()
                .ForMember(dto => dto.Id, obj => obj.MapFrom(order => order.Id))
                .ForMember(dto => dto.AddressId, obj => obj.MapFrom(order => order.Address.Id))
                .ForMember(dto => dto.CustomerId, obj => obj.MapFrom(order => order.Customer.Id))
                .ReverseMap();
        }).CreateMapper();

And object:

        var customerTest = new Customer
        {
            Id = Guid.NewGuid(),
            Addresses = new List<Address>
            {
                new Address
                {
                    Id = Guid.NewGuid(),
                    Customer = new Customer{ Id = Guid.NewGuid() }
                }
            },
            Orders = new List<Order>
            {
                new Order
                {
                    Id = Guid.NewGuid(),
                    Customer = new Customer{ Id = Guid.NewGuid() },
                    Address = new Address{ Id = Guid.NewGuid() }
                }
            }
        };

You could do the following:

        var resultDto = mapper.Map<CustomerDto>(customerTest);
        var resultModel = mapper.Map<Customer>(resultDto);


来源:https://stackoverflow.com/questions/47723564/how-to-reverse-map-to-an-existing-parent-object-using-automapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!