I am using mapster and C# in visual studio and have three objects I need to map to one. Has anyone done this, any examples? Mapster says it has that capability but I can't get it to work. Thanks.
Asked
Active
Viewed 5,228 times
2 Answers
6
You can create a new TypeAdapterConfig with a Tuple of all the object as Source. Here is an example
public class DTO1
{
public int Age { get; set; }
}
public class DTO2
{
public int ID { get; set; }
}
public class DTO3
{
public string Name { get; set; }
}
public class POCO
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
var config = TypeAdapterConfig<(DTO1, DTO2, DTO3), POCO>.NewConfig()
.Map(dest => dest.Age, src => src.Item1.Age)
.Map(dest => dest.ID, src => src.Item2.ID)
.Map(dest => dest.Name, src => src.Item3.Name);
Considering DTO1, DTO2, DTO3 as your source entities that you want to map to the POCO entity.
Once you have defined this configuration you can pass it as a paramter (config.Config) to the Adapt method.

Manoj
- 604
- 4
- 11
-
6If you were idiot like me and wondering how do you actually call Adapt with multiple sources, here is how: (src1,src2).Adapt
() – PatrickBateman92 Jul 28 '22 at 09:44
0
So, if you have to create a complete mapper profile for every property, then a mapper doesn't make sense, use extensions or adapters for that

Teo
- 1
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 07 '23 at 18:11
T destination = (src1, src2, src3).Adapt();
but it is not working. My mapper configuration :
TypeAdapterConfig<(object1, object2, object3),
Tdestination>
.NewConfig()
.Map(dest => dest.x, src => src.item1.a)
.Map(dest => dest.y, src => src.item2.a)
.Map(dest => dest.z, src => src.item3.a)
– jaykum Jun 21 '21 at 23:44