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
Connecting Azure Blob Storage account using Managed Identity
Posted 12/9/2022Securing Azure KeyVault connections using Managed Identity
Posted 11/26/2022Manage application settings with Azure KeyVault
Posted 11/9/2022Adding Serilog to Azure Functions created using .NET 5
Posted 4/3/2021Learn how to split log data into different tables using Serilog in ASP.NET Core
Posted 4/23/2020