0
votes

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:

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:

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. I have tried with following Automapper configurations:

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:

{
    "people": [
        {
            "id": 213,
            "firstName": "John",
            "lastName": "Doe",
            "email": "[email protected]",
            "profession": "Full time professor Biology",
            "jobId": "J3487H"
        },
        {
            "id:" 928,
            "firstName": "Mary",
            "lastName": "Doe",
            "email": "[email protected]",
            "profession": "Part time professor Chemistry",
            "jobId": "P3436JJ"
        }
    ]
}

How could I get mapped destination model from the source one? I cannot change structure from my destination model because is defined in existing .dll library. I also didn't find any solutions in the documentation about inherited abstract classes and interfaces.

Thanks everyone!

1
Could you elaborate on what "does not work" mean? Are you getting exceptions? Syntax errors?Xerillio

1 Answers

-1
votes

I have reviewed some StackOverflow answers and finally came with following workable solution.

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 :)