不可思议的扩展行为不同于Automapper多态收集

时间:2015-04-22 03:30:38

标签: linq polymorphism iqueryable automapper-3

使用Automapper 3.3.1.0,Mapper.Map<IEnumerable<TDestination>>(someEnumerable)someEnumerable.AsQueryable().Project().To<TDestination>()的使用之间存在不同的映射行为

这似乎不是SQL LINQ提供程序或其他提供者的限制,因为这是在内存中的集合中见证的。

与许多事情一样,最好用例子来解释:

注意: 以下代码可在https://gist.github.com/kmoormann/b3949d006f4083ab6ee4找到

using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FluentAssertions;
using NUnit.Framework;

namespace Automapper.PolymorphicList.Tests
{
    [TestFixture]
    class AutomapperQueryableExtensionPolymorphism
    {
        //taking the class structure from: https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance
        public class Order { }

        public class OnlineOrder : Order
        {
            public string Referrer { get; set; }
        }
        public class MailOrder : Order { }

        //Dtos
        public class OrderDto
        {
            public string Referrer { get; set; }
        }

        [Test(Description = "Does the same mapping behavior exist for a polymorphic list when doing the project querable extension as when doing the static mapper map method()")]
        public void IsSameBehaviorForQueryableExtensionAndStaticMap()
        {
            Mapper.Reset();
            //Mappings
            Mapper.CreateMap<Order, OrderDto>()
                .Include<OnlineOrder, OrderDto>()
                .Include<MailOrder, OrderDto>()
                .ForMember(o => o.Referrer, m => m.Ignore());
            Mapper.CreateMap<OnlineOrder, OrderDto>();
            Mapper.CreateMap<MailOrder, OrderDto>();

            //build lists
            var onlineOrders = new List<OnlineOrder>() { new OnlineOrder() { Referrer = "one" }, new OnlineOrder() { Referrer = "two" } };
            var mailOrders = new List<MailOrder>() { new MailOrder() };

            //single typed list mapping
            var mappedOnlineOrderDtos = Mapper.Map<IEnumerable<OrderDto>>(onlineOrders);
            var projectedOnlineOrderDtos = onlineOrders.AsQueryable().Project().To<OrderDto>();

            //using FluentAssertions for collection assertions
            projectedOnlineOrderDtos.ShouldBeEquivalentTo(mappedOnlineOrderDtos, "automapper can handle singly typed lists");


            //other single typed list mapping
            var mappedMailOrderDtos = Mapper.Map<IEnumerable<OrderDto>>(mailOrders);
            var projectedMailOrderDtos = mailOrders.AsQueryable().Project().To<OrderDto>();

            projectedMailOrderDtos.ShouldBeEquivalentTo(mappedMailOrderDtos, "automapper can handle singly typed lists");

            //build a polymorphic list
            var orders = new List<Order>();
            orders.AddRange(onlineOrders);
            orders.AddRange(mailOrders);

            // Perform Mapping and Projection
            var mappedPolymorhpicOrders = Mapper.Map<IEnumerable<OrderDto>>(orders);
            var projectedPolymorphicOrders = orders.AsQueryable().Project().To<OrderDto>();

            projectedPolymorphicOrders.ShouldBeEquivalentTo(mappedPolymorhpicOrders, "automapper can handle polymorphic typed lists?");

        }
    }
}

我理解.Project().To<TDestination> IQueryable扩展名有limitations但我不知道的是:

  • 哪种限制导致了这种行为?
  • 这是Automapper限制或LINQ限制
  • 还有一项工作仍然是使用可查询扩展名而不是仅仅还原Mapper.Map<TDestination>(obj)吗?

后人:link讨论话题主题

1 个答案:

答案 0 :(得分:1)

这是LINQ限制。 AutoMapper不继承LINQ的基本映射。什么是Select表达式做多态选择投影?尝试这样做可以将您带到您无法做到的地方。 LINQ查询提供商不支持它。