我需要實現一個可插入系統,其中Automapper配置文件可以由許多DLL提供。Automapper - 可插入映射
被映射對象具有人的列表:
public class CompanySrc
{
public List<PersonSrc> Persons {get;set;}
}
public class CompanyDest
{
public List<PersonDest> Persons {get;set;}
}
PersonSrc和PersonDest是可以在每個DLL被擴展的抽象類:
DLL1:
public class EmployeeSrc : PersonSrc
{
...
}
public class EmployeeDest : PersonDest
{
...
}
DLL2 :
public class ManagerSrc : PersonSrc
{
...
}
public class ManagerDest : PersonDest
{
...
}
當時的想法是,以實現類似於此:
public class DLL1Profile : Profile
{
public DLL1Profile()
{
CreateMap<PersonSrc, PersonDest>()
.Include<EmployeeSrc, EmployeeDest>();
CreateMap<EmployeeSrc, EmployeeDest>();
}
}
public class DLL2Profile : Profile
{
public DLL2Profile()
{
CreateMap<PersonSrc, PersonDest>()
.Include<ManagerSrc, ManagerDest>();
CreateMap<ManagerSrc, ManagerDest>();
}
}
映射在下列方式
var mc = new MapperConfiguration(cfg =>
{
cfg.CreateMap<CompanySrc, CompanyDest>()
cfg.AddProfile(new DLL1Profile());
cfg.AddProfile(new DLL2Profile());
});
IMapper sut = mc.CreateMapper();
var result = sut.Map<CompanyDest>(companySrc);
完成,但這種方法是行不通的。當「人員」列表中包含員工和經理時,我試圖映射整個列表,我得到一個異常。 有什麼建議嗎?
你,因爲多態性是什麼意思?所以你有一個單獨的列表,裏面有'People',有些列表可能是'Manager',有些可能是'Employee'? 如果是這種情況,應用你的'Profile'會導致一個異常,因爲當列表包含兩種類型時,它將嘗試映射到'Manager'或'Employee'... 說到這一點,我認爲Automapper不會映射在Dest上不存在的道具,而是拋出異常。也許這個問題是缺少一些信息(即什麼是異常消息?) –
我編輯了更多信息的問題。拋出的異常是誤導,因爲它似乎映射器不能映射一個字段,但內部有一個對象引用沒有設置,因爲可能automapper試圖創建一個「Person」的實例,但因爲它是抽象的不能。 – Luca
看: https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance 和 https://stackoverflow.com/questions/39884805/polymorphic-mapping-of-collections-with- automapper –