Automapper继承映射工厂
Automapper inheritance mapping factory
我有以下 classes:
public class A{}
public class AA : A {public int aa{get;set;}}
public class AAA : A {public int aaa{get;set;}}
public class B{}
public class BB : B {public int bb{get;set;}}
public class BBB : B {public int bbb{get;set;}}
这是我的映射:
Mapper.CreateMap<A,B>().include<AA,BB>().include<AAA,BBB>();
Mapper.CreateMap<AA,BB>();
Mapper.CreateMap<AAA,BBB>();
我假设 automapper 会理解层次结构并映射适当的派生 class。但是我无法使用下面的代码获得它。映射实体始终是基础 class,在本例中为 - B。
请注意,我有一个存储库工厂,它将确定 GetAll()
的 return 类型。
var list = myRepo.GetAll() //this returns IEnumerabe<AA> or IEnumerable<AAA>
var newlist = Mapper.Map<IEnumerable<B>>(list); //I wanted to use "B" here due to the fact I will have many inherited classes of B. I don't want to restrict to a specific child class of B.
好的。上面的代码应该可以工作。不幸的是,我使用的是 automapper 4.1.1 并且有一个错误 -https://github.com/AutoMapper/AutoMapper/issues/819.
我有以下 classes:
public class A{}
public class AA : A {public int aa{get;set;}}
public class AAA : A {public int aaa{get;set;}}
public class B{}
public class BB : B {public int bb{get;set;}}
public class BBB : B {public int bbb{get;set;}}
这是我的映射:
Mapper.CreateMap<A,B>().include<AA,BB>().include<AAA,BBB>();
Mapper.CreateMap<AA,BB>();
Mapper.CreateMap<AAA,BBB>();
我假设 automapper 会理解层次结构并映射适当的派生 class。但是我无法使用下面的代码获得它。映射实体始终是基础 class,在本例中为 - B。
请注意,我有一个存储库工厂,它将确定 GetAll()
的 return 类型。
var list = myRepo.GetAll() //this returns IEnumerabe<AA> or IEnumerable<AAA>
var newlist = Mapper.Map<IEnumerable<B>>(list); //I wanted to use "B" here due to the fact I will have many inherited classes of B. I don't want to restrict to a specific child class of B.
好的。上面的代码应该可以工作。不幸的是,我使用的是 automapper 4.1.1 并且有一个错误 -https://github.com/AutoMapper/AutoMapper/issues/819.