IQueryable Extension Behavior Differing For Automapper Polymorphic Collection

爱⌒轻易说出口 提交于 2019-12-22 01:18:45

问题


Using Automapper 3.3.1.0 there is a different mapping behavior between the usage of Mapper.Map<IEnumerable<TDestination>>(someEnumerable) compared to someEnumerable.AsQueryable().Project().To<TDestination>()

This does not appear to be a limitation of a SQL LINQ provider or other as this is witnessed in an in-memory collection.

As with many things this is best explained by example:

Note: the following code can be found at 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?");

        }
    }
}

I understand there are limitations to the .Project().To<TDestination> IQueryable extensions but what I am unaware of is:

  • which limitation is causing this behavior?
  • is this a Automapper limitation or a LINQ limitation
  • is there a work around to still use the queryable extensions and not revert Mapper.Map<TDestination>(obj) exclusively?

for posterity: link discussion thread topic


回答1:


This is a LINQ limitation. AutoMapper does not inherit base mappings for LINQ. What would be the Select expression to do polymorphic Select projections? Trying to do that leads you to where you can't do this. LINQ query providers don't support it.



来源:https://stackoverflow.com/questions/29787197/iqueryable-extension-behavior-differing-for-automapper-polymorphic-collection

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