1

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.

jaykum
  • 138
  • 3
  • 10
  • You are going to need to tell us a little (well, a lot) more. That's a very low-information question. For example, are the three objects of the same class? What about the "one"? What kind of mapping rules are you talking about? What does your code look like? – Flydog57 Jun 21 '21 at 22:53
  • I have three different objects with various properties and I have custom mapping logic to map the properties from each to different parts of the destination object. The adapt statement is: 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
  • By _"I have three different objects with various properties"_, I take it to mean you have 4 different classes (three source and another destination) and you want to build a mapping that picks out some properties (and values) from objects of each of the source classes and use those to populate an object of the fourth class. You will want to show a [mcve] that shows three source classes and a destination class and an explanation of what you want to do. I know nothing of _Mapster_ - I'm just trying to improve your question so someone else might answer it – Flydog57 Jun 22 '21 at 00:01
  • Show your formatted code in your question. – mxmissile Aug 20 '21 at 17:03

2 Answers2

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
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
  • 1
    Your 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