AutoMapper - Custom Mapping

Up untill now we have gone various examples using AutoMapper that was doing one-to-one mapping. But there may arise some scenarios where we need to apply some custom logic for doing the mapping. We can do that in AutoMapper in multiple ways, one way of doing that is using ResolveUsing method while creating a map

In this example I have used lambda expression in ResolveUsing method for appending the FirstName and LastName property of the source and maps it to the Name property in the destination object. You can see from the result box that it's showing correctly after the mapping operation.

Another way of doing mapping is by making use of ValueResolvers class instead of lambda expression in ResolveUsing. For that you will need to create a class which inherits the ValueResolvers class and override the method to write the custom logic needed for your mapping. Also there is slight change in ResolveUsing method to call our custom value resolver

Custom resolvers are normally used whenever there is a need to implement a complex logic where as for simple ones it's better to follow lambda expressions or use MapFrom method as shown below.


No Comments

Add a Comment

Recent Posts