How to use Automapper to create complex viewmodel objects from a simple object and a method call

陌路散爱 提交于 2019-12-29 09:28:22

问题


My application needs to display a table of customer data, including data about the customer and about his most recent order from a given warehouse. The customer domain object contains a GetLatestOrder(warehouseId) method.

I have a CustomerView viewmodel and want to be able to populate it with some fields from the customer object, and a few fields from the latest order object. Can I do this using Automapper?


回答1:


Try this,

In Global.asax.cs [Application_Start], public static void AppInitialize()

    protected void Application_Start(object sender, EventArgs e)
    {
        Mapper.CreateMap<Order, CustomerViewModel>()
             .ForMember(destination => destination.OrderId, options => options.MapFrom(source => source.Id))
             .ForMember(destination => destination.OrderName, options => options.MapFrom(source => source.Name));
        Mapper.CreateMap<Customer, CustomerViewModel>()
             .ForMember(destination => destination.CustmerId, options => options.MapFrom(source => source.Id))
             .ForMember(destination => destination.CustmerName, options => options.MapFrom(source => source.Name))
             .ForMember(destination => destination.OrderId, options => options.MapFrom(source => Mapper.Map<IEnumerable<Order>, IEnumerable<CustomerViewModel>>(source.Orders).FirstOrDefault().OrderId))
             .ForMember(destination => destination.OrderName, options => options.MapFrom(source => Mapper.Map<IEnumerable<Order>, IEnumerable<CustomerViewModel>>(source.Orders).FirstOrDefault().OrderName));
    }

And in your code call the mapper like below,

        var lstCustomers = new List<Customer>
        {
            new Customer { Id = 1, Name="Name", Orders = new List<Order> { new Order { Id = 1000, Name ="Some Name"}}},
        };

        var result = Mapper.Map<IEnumerable<Customer>, IEnumerable<CustomerViewModel>>(lstCustomers);

I am presumed that you classes looks like below,

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class CustomerViewModel
{
    public int CustmerId { get; set; }
    public string CustmerName { get; set; }
    public int OrderId { get; set; }
    public string OrderName { get; set; }
}

Also refer this post.




回答2:


Yes, you can:

var vm = new CustomerViewModel();
Mapper.Map(customer, vm);
Mapper.Map(order, vm);

Each mapping will populate the properties it is configured to and leave the rest.




回答3:


In the end, I took the easy approach, couldn't find another way:

In Application_start:

    Mapper.CreateMap<Customer, CustomerView>();
    Mapper.CreateMap<Order, CustomerView>();

In the controller method:

    IEnumerable<Customer> customers = GetCustomers(false)
                                               .OrderBy(c => c.Name);
    IEnumerable<CustomerView> viewData = Mapper.Map<IEnumerable<Customer>, IEnumerable<CustomerView>>(customers);
    foreach (Customer customer in customers)
    {
        CustomerView view = viewData.Where(cv => cv.CustomerId == customer.CustomerId).First();
        Mapper.Map(customer.GetLatestOrder(WarehouseId), view);
    }


来源:https://stackoverflow.com/questions/13348812/how-to-use-automapper-to-create-complex-viewmodel-objects-from-a-simple-object-a

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