简体   繁体   English

使用 Automapper 映射复杂的继承模型

[英]Mapping complex inherited models with Automapper

I am working on my .NET Core 3.0 app and I need to map my model into another model with Automapper (I have ddl-s and need to use those libraries). I am working on my .NET Core 3.0 app and I need to map my model into another model with Automapper (I have ddl-s and need to use those libraries).

My model (source model) has following structure:我的 model(源模型)具有以下结构:

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; }
}

Destination model is following:目的地 model 如下:

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; }
}

I was not able to get right mapping into destination model.我无法正确映射到目标 model。 I have tried with following Automapper configurations:我尝试了以下 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());
});

The response I need to map from source into destination model is following:我需要对 map 从源到目标 model 的响应如下:

{
    "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"
        }
    ]
}

How could I get mapped destination model from the source one?如何从源头获取映射的目标 model? I cannot change structure from my destination model because is defined in existing.dll library.我无法从目标 model 更改结构,因为在 existing.dll 库中定义。 I also didn't find any solutions in the documentation about inherited abstract classes and interfaces.我在文档中也没有找到关于继承抽象类和接口的任何解决方案。

Thanks everyone!感谢大家!

I have reviewed some StackOverflow answers and finally came with following workable solution.我已经查看了一些 StackOverflow 答案,最后提出了以下可行的解决方案。

I have created custom converter:我创建了自定义转换器:

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);
        }
    }

which was then included into existing mapper configuration:然后将其包含到现有的映射器配置中:

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<src.Professor, dest.IPerson>()
       .ConvertUsing(new ProfessorIPersonConverter());
    cfg.CreateMap<src.Professor, dest.TeachingProfessor>();
});

It works like a charm:)它就像一个魅力:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM