使用 Automapper 映射复杂的继承模型
Mapping complex inherited models with Automapper
我正在开发我的 .NET Core 3.0 应用程序,我需要使用 Automapper 将我的模型映射到另一个模型(我有 ddl-s 并且需要使用这些库)。
我的模型(源模型)具有以下结构:
public class ProfessorModel {
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Profession { get; set; }
public string JobId { get; set; }
}
public class UniversityMembersModel {
public IEnumerable<ProfessorModel> People { get; set; }
}
目标模型如下:
public interface Identificator {
int Id { get; }
}
public interface IPerson : Identificator {
string FirstName { get; }
string LastName { get; }
}
public abstract class TeachingStaff : IPerson, Identificator {
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
public class TeachingProfessor : TeachingStaff {
public string Profession { get; set; }
public string TeachingSubject { get; set; }
public string JoblId { get; set; }
}
public class UniversityMembers {
public IEnumerable<IPerson> People { get; set; }
}
我无法正确映射到目标模型。我尝试使用以下 Automapper 配置:
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<ProfessorModel, IPerson>(); // <-- does not work
// does not work:
cfg.CreateMap<ProfessorModel, IPerson>()
.ConstructUsing(p => new TeachingProfessor());
// does not work:
cfg.CreateMap<ProfessorModel, TeachingProfessor>();
// does not work:
cfg.CreateMap<ProfessorModel, TeachingProfessor>()
.ConstructUsing(p => new TeachingProfessor());
});
我需要从源映射到目标模型的响应如下:
{
"people": [
{
"id": 213,
"firstName": "John",
"lastName": "Doe",
"email": "johny@myUni.edu",
"profession": "Full time professor Biology",
"jobId": "J3487H"
},
{
"id:" 928,
"firstName": "Mary",
"lastName": "Doe",
"email": "mary.me@my.uni",
"profession": "Part time professor Chemistry",
"jobId": "P3436JJ"
}
]
}
如何从源模型中获取映射的目标模型?我无法更改目标模型的结构,因为它是在现有 .dll 库中定义的。
我也没有在文档中找到有关继承抽象 类 和接口的任何解决方案。
谢谢大家!
我查看了一些 Whosebug 的答案并最终得出了以下可行的解决方案。
我已经创建了自定义转换器:
public class ProfessorIPersonConverter : ITypeConverter<src.Professor, dest.IPerson>
{
public IPerson Convert(Professor source, IPerson destination, ResolutionContext context)
{
return context.Mapper.Map<src.Professor, dest.TeachingProfessor>(source);
}
}
然后包含在现有映射器配置中:
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<src.Professor, dest.IPerson>()
.ConvertUsing(new ProfessorIPersonConverter());
cfg.CreateMap<src.Professor, dest.TeachingProfessor>();
});
它就像一个魅力:)
我正在开发我的 .NET Core 3.0 应用程序,我需要使用 Automapper 将我的模型映射到另一个模型(我有 ddl-s 并且需要使用这些库)。
我的模型(源模型)具有以下结构:
public class ProfessorModel {
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Profession { get; set; }
public string JobId { get; set; }
}
public class UniversityMembersModel {
public IEnumerable<ProfessorModel> People { get; set; }
}
目标模型如下:
public interface Identificator {
int Id { get; }
}
public interface IPerson : Identificator {
string FirstName { get; }
string LastName { get; }
}
public abstract class TeachingStaff : IPerson, Identificator {
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
public class TeachingProfessor : TeachingStaff {
public string Profession { get; set; }
public string TeachingSubject { get; set; }
public string JoblId { get; set; }
}
public class UniversityMembers {
public IEnumerable<IPerson> People { get; set; }
}
我无法正确映射到目标模型。我尝试使用以下 Automapper 配置:
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<ProfessorModel, IPerson>(); // <-- does not work
// does not work:
cfg.CreateMap<ProfessorModel, IPerson>()
.ConstructUsing(p => new TeachingProfessor());
// does not work:
cfg.CreateMap<ProfessorModel, TeachingProfessor>();
// does not work:
cfg.CreateMap<ProfessorModel, TeachingProfessor>()
.ConstructUsing(p => new TeachingProfessor());
});
我需要从源映射到目标模型的响应如下:
{
"people": [
{
"id": 213,
"firstName": "John",
"lastName": "Doe",
"email": "johny@myUni.edu",
"profession": "Full time professor Biology",
"jobId": "J3487H"
},
{
"id:" 928,
"firstName": "Mary",
"lastName": "Doe",
"email": "mary.me@my.uni",
"profession": "Part time professor Chemistry",
"jobId": "P3436JJ"
}
]
}
如何从源模型中获取映射的目标模型?我无法更改目标模型的结构,因为它是在现有 .dll 库中定义的。 我也没有在文档中找到有关继承抽象 类 和接口的任何解决方案。
谢谢大家!
我查看了一些 Whosebug 的答案并最终得出了以下可行的解决方案。
我已经创建了自定义转换器:
public class ProfessorIPersonConverter : ITypeConverter<src.Professor, dest.IPerson>
{
public IPerson Convert(Professor source, IPerson destination, ResolutionContext context)
{
return context.Mapper.Map<src.Professor, dest.TeachingProfessor>(source);
}
}
然后包含在现有映射器配置中:
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<src.Professor, dest.IPerson>()
.ConvertUsing(new ProfessorIPersonConverter());
cfg.CreateMap<src.Professor, dest.TeachingProfessor>();
});
它就像一个魅力:)