I'm trying to replace this manual mapping:
var group = new Group {
Participants = family.Children.ToDictionary(child => $"{child.LastName}, {child.FirstName}", child =>
new Participant
{
Class = child.Age,
})
};
to using Mapster: var camp = new UnitMapper().Map(family)
.
But how do I make it do the Dictionary
's mapping correctly?
These is what I currently have:
[Mapper]
public interface IUnitMapper {
Group Map(Family family);
}
public class MappingRegistration : IRegister {
public void Register(TypeAdapterConfig config) {
config.NewConfig<Family, Group>()
.Map(to => to.Participants, from => from.Children)
;
config.NewConfig<Child, Participant>()
.Map(to => to.Class, from => from.Age)
;
}
}
public class Family {
public IEnumerable<Child> Children { get; set; }
}
public class Child {
public string FirstName { get; init; }
public string LastName { get; init; }
public int Age { get; init; }
}
public class Group {
public IDictionary<string, Participant> Participants { get; set; }
}
public class Participant {
public int Class { get; set; }
}