Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapster map using multiple objects to one

Tags:

c#

mapster

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.

like image 983
jaykum Avatar asked Nov 27 '25 12:11

jaykum


1 Answers

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.

like image 133
Manoj Avatar answered Nov 29 '25 04:11

Manoj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!