通过AutoMapper将IList映射到ICollection [英] Map IList to ICollection through AutoMapper

查看:107
本文介绍了通过AutoMapper将IList映射到ICollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Order
{
    public int OrderId { get; set; }
    public string OrderCode { get; set; }
    public IList<OrderItem> OrderItems { get; set; }
}

public class OrderDTO
{
    public string OrderId { get; set; }
    public string IdentifiableCode { get; set; }
    public decimal TotalCost { get; set; }
    public ICollection OrderItems { get; set; }
}

public class OrderItem
{
    public int OrderItemId { get; set; }
    public int ItemCount { get; set; }
    public Order Order { get; set; }
}

public class OrderItemDTO
{
    public int OrderItemId { get; set; }
    public int ItemCount { get; set; }
    public OrderDTO Order { get; set; }
}

在这种情况下,如何将OrderItems的IList映射到ICollection.我尝试使用解析器,但由于OrderItem具有Order引用而以递归方式结束.

In this scenario how can i map IList of OrderItems to ICollection. I tried to use resolver but ended up in recursive since OrderItem has Order reference.

有什么想法吗?

推荐答案

首先,我对您的类进行了一些调整.一种是添加用于初始化集合的构造函数,这样我就可以在测试中添加它们.其次,我不明白为什么您希望OrderDTO.OrderItems成为松散类型的ICollection.如果这样做,Automapper只会将IList分配给ICollection(因为IList实现了ICollection).如果将其定义为IList,则Automapper将看到它知道如何从OrderItem转换为OrderItemDTO,并将映射集合的每个成员.

First off, I added a few tweaks to your classes. One was to add constructors that initialize the collections so I could add to them in my test. Second, I don't see why you'd want the OrderDTO.OrderItems to be a loosely-typed ICollection. If you do that, Automapper just assigns the IList to the ICollection (since IList implements ICollection). If you define it as IList, Automapper will see that it knows how to convert from OrderItem to OrderItemDTO and will map each member of the collection.

我还必须添加一些Ignore(),因为OrderItemDTO不包含IdentifiableCode或TotalCost(不确定要对它们做什么).最后,可以通过AfterMap()中的简单foreach循环来完成父/子映射(OrderItemDTO对其父对象有引用).所以这是我想出的映射:

I also had to add some Ignore()'s since OrderItemDTO doesn't contain IdentifiableCode nor TotalCost (not sure what you wanted to do with those). Finally, the parent/child mapping (OrderItemDTO having a reference to its parent) can be done by a simple foreach loop in the AfterMap(). So here's the mapping I came up with:

Mapper.CreateMap<Order, OrderDTO>()
    .ForMember(d => d.IdentifiableCode, o => o.Ignore())
    .ForMember(d => d.TotalCost, o => o.Ignore())
    .AfterMap((s, d) =>
                {
                    foreach (var l in d.OrderItems)
                    {
                        l.Order = d;
                    }
                });
Mapper.CreateMap<OrderItem, OrderItemDTO>();
Mapper.AssertConfigurationIsValid();

这是我用来检查问题的测试:

And here's the test I used to check things out:

var order = new Order
                {
                    OrderCode = "AAA",
                    OrderId = 22,
                    OrderItems = new List<OrderItem>(),
                };
order.OrderItems.Add(new OrderItem { ItemCount = 2, Order = order, OrderItemId = 33 });
order.OrderItems.Add(new OrderItem { ItemCount = 1, Order = order, OrderItemId = 99 });

var mapped = Mapper.Map<Order, OrderDTO>(order);

这篇关于通过AutoMapper将IList映射到ICollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆