Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two objects to produce third using AutoMapper

Tags:

automapper

I know it's AutoMapper and not AutoMerge(r), but...

I've started using AutoMapper and have a need to Map A -> B, and to add some properties from C so that B become a kind of flat composite of A + C.

Is this possible in AutoMapper of should I just use AutoMapper to do the heavy lifting then manually map on the extra properties?

like image 539
Jason Hyland Avatar asked Mar 17 '10 14:03

Jason Hyland


People also ask

How do you combine two objects?

To merge objects into a new one that has all properties of the merged objects, you have two options: Use a spread operator ( ... ) Use the Object. assign() method.

What is the use of AutoMapper in C#?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.

What is AutoMapper good for?

AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.


1 Answers

Would this not work?

var mappedB = _mapper.Map<A,B>(aInstance); _mapper.Map(instanceC,mappedB); 
like image 81
epitka Avatar answered Nov 11 '22 20:11

epitka