2017-07-21 14 views
1

AutoMapper v6.1.1を使用して投影を使用してクラスをマップしようとしていますが、AutoMapperには深くネストされたオブジェクトは含まれていません。Automapper投影には深くネストされた階層オブジェクトが含まれていません

私はここでユニットテストとの完全なのVisual Studio 2015ソリューション添付しました:「AREN https://www.dropbox.com/s/omue5ou5dvxsa57/UnitTestProject2.zip?dl=0

私は基本的に Person階層に ChildParent階層をマップしようとしている

が、grand- Parentsを投影結果に含めること。

モデル:

public class Child 
{ 
    public string Name { get; set; } 

    public virtual Parent Parent { get; set; } 
} 

public class Parent 
{ 
    public string Name { get; set; } 

    public virtual Parent GrandParent { get; set; } 
} 

public class Person 
{ 
    public string Name { get; set; } 

    public virtual Person Parent { get; set; } 
} 

マッピングプロファイル:

public class PersonProfile : Profile 
{ 
    public PersonProfile() 
    { 
     this.CreateMap<Child, Person>() 
      .MaxDepth(5); 
     this.CreateMap<Parent, Person>() 
      .ForMember(destinationMember => destinationMember.Parent, memberOptions => memberOptions.MapFrom(sourceMember => sourceMember.GrandParent)) 
      .MaxDepth(5); 
    } 
} 

ユニットテスト:

Mapメソッドの作品を​​使用して
[TestClass] 
public class UnitTest1 
{ 
    IMapper mapper; 
    List<Child> children; 

    [TestInitialize] 
    public void TestInitialize() 
    { 
     MapperConfiguration configuration = new MapperConfiguration((config => 
     { 
      config.AddProfile(new PersonProfile()); 
      config.ForAllMaps((mapType, mapperExpression) => 
      { 
       mapperExpression.MaxDepth(5); 
      }); 
     })); 

     this.mapper = configuration.CreateMapper(); 

     mapper.ConfigurationProvider.AssertConfigurationIsValid(); 

     this.children = new List<Child> 
     { 
      new Child 
      { 
       Name = "Child1", 
       Parent = new Parent 
       { 
        Name = "Parent1", 
        GrandParent = new Parent 
        { 
         Name = "GrandParent1", 
         GrandParent = new Parent 
         { 
          Name = "GreatGrandParent1" 
         } 
        } 
       } 
      } 
     }; 
    } 

    [TestMethod] 
    public void TestProjection() 
    { 
     IQueryable<Person> people = children.AsQueryable().ProjectTo<Person>(mapper.ConfigurationProvider); 

     AssertPeople(people); 
    } 

    [TestMethod] 
    public void TestMap() 
    { 
     List<Person> people = mapper.Map<List<Child>, List<Person>>(children); 

     AssertPeople(people.AsQueryable()); 
    } 

    private void AssertPeople(IQueryable<Person> people) 
    { 
     Assert.IsNotNull(people); 
     Assert.AreEqual(1, people.Count()); 

     Person child1 = people.ElementAt(0); 
     Assert.AreEqual("Child1", child1.Name); 

     Person parent1 = child1.Parent; 
     Assert.IsNotNull(parent1); 
     Assert.AreEqual("Parent1", parent1.Name); 

     Person grandParent1 = parent1.Parent; 
     Assert.IsNotNull(grandParent1); // fails when using ProjectTo 
     Assert.AreEqual("GrandParent1", grandParent1.Name); 
    } 
} 

しかしProjectToません。

サンプルコードのクラスは、プロダクションで使用されるクラスよりはるかに簡単です。

ODataからIQueryable<Person>を返信し、によって生成されたSQLを利用できるように、プロジェクションを使用しようとしています。クエリオプションが自動的に適用されます。

何か助けていただければ幸いです。

ありがとうございました!

+0

私はそれが午前問題だとは思わない。現在のところ、(再帰ラムダ代理人ではなく)再帰式を定義して翻訳する方法はありません。 –

答えて

0

私は、この問題について説明だと思う: https://github.com/AutoMapper/AutoMapper/issues/2171

しかし、回避策として、基本的に内部的に地図を呼び出す拡張メソッドを作成することはできません。

public static class Extenstions 
{ 
    public static IQueryable<TDestination> ProjectToExt<TDestination, TSource>(this IQueryable<TSource> @this, 
     IMapper mapper) 
    { 
     return mapper.Map<IEnumerable<TDestination>>(@this).AsQueryable(); 
    } 
} 

そして、呼び出し元のコードは次のようです:

IQueryable<Person> people = children.AsQueryable().ProjectToExt<Person, Child>(mapper); 
関連する問題