0

I have the following two POCOs:

private class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

private class PersonDto
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Then I defined two people:

private readonly Person _person1 = new Person
{
    Name = "Steve",
    Age = 20
};

private readonly Person _person2 = new Person
{
    Name = "Alex",
    Age = 45
};

Here is the code I tried to execute:

var mapper = new MapperConfiguration(m =>
{
    m.CreateMap<Person, PersonDto>().ReverseMap();
    m.CreateMap(typeof(Tuple<>), typeof(Tuple<>)).ReverseMap();
}).CreateMapper();

var tuple = Tuple.Create(_person1, _person2);
var mappedTuple = mapper.Map<Tuple<PersonDto, PersonDto>>(tuple);

On execution, I get an AutoMapperMappingException saying I am missing a type map configuration or the mapping is unsupported. My hope was that I could leverage the open generics feature and not have to register every Tuple version. If I explicitly do this, then everything works fine. Am I missing anything?

I am using AutoMapper v10.

Collin Alpert
  • 475
  • 6
  • 14

1 Answers1

2

It's because you did not specfied the generics of Tuple

var mapper = new MapperConfiguration(m =>
{
    m.CreateMap<Person, PersonDto>().ReverseMap();
    m.CreateMap(typeof(Tuple<Person, Person>), typeof(Tuple<PerdonDto,PersonDto>)).ReverseMap();
}).CreateMapper();

var tuple = Tuple.Create(_person1, _person2);
var mappedTuple = mapper.Map<Tuple<PersonDto, PersonDto>>(tuple);

Edit

the right thing to do is:

var mapper = new MapperConfiguration(m =>
    {
        m.CreateMap<Person, PersonDto>().ReverseMap();
        m.CreateMap(typeof(Tuple<,>), typeof(Tuple<,>)).ReverseMap();
    }).CreateMapper();
    
    var tuple = Tuple.Create(_person1, _person2);
    var mappedTuple = mapper.Map<Tuple<PersonDto, PersonDto>>(tuple);
ddfra
  • 2,413
  • 14
  • 24
  • This was my code before and it's exactly what I was trying to avoid. I am looking for a way to not have to register every generic type explicitly. Same thing for an `IReadOnlyCollection`, for example. My hope is to get the [open generics](https://docs.automapper.org/en/latest/Open-Generics.html) feature to work. – Collin Alpert Jul 13 '20 at 12:52
  • 1
    I understand, try Tuple < , > insted of T< > (there is one comma) – ddfra Jul 13 '20 at 12:58
  • Yes, of course!! I can't believe I missed that. It works now. Thanks! – Collin Alpert Jul 13 '20 at 13:00