AutoMapper - Working with prefix and postfix
Another configuration option we have is for recognizing prefixes or postfixes in the member names. In some scenarios we may have a prefix or postfix for all the properties in the source model and it's not there in the destination model. This cannot be handled by the default mapping rules in AutoMapper. For that we have RecognizePrefixes and RecognizePostfixes method in the MapperConfiguration class where we can specify postfix or prefix. If AutoMapper finds the prefix/postfix in the members in source instanace then it will be discarded and if it found a match in the destination instance then maps correctly.
Syntax - Prefix
RecognizePrefixes("<prefix name>");
Syntax - Postfix
RecognizePrefixes("<postfix name>");
Sample Code - Prefix
var config = new MapperConfiguration(cfg => { cfg.RecognizePrefixes("Work"); cfg.CreateMap<ModelData, ViewModel>(); });
Sample Code - Postfix
var config = new MapperConfiguration(cfg =>
{
cfg.RecognizePostfixes("Work");
cfg.CreateMap<ModelData, ViewModel>();
});
In the example given below, we have a prefix Work for all the properties in the source model which in turn is not matching with the names in the destination model. So AutoMapper will throw an exception if we run this without adding RecongnizePrefixes method in configuration. So to ignore this prefix from considering for mapping, we need to call this method on MappingConfiguration object as shown in the example
using System; using AutoMapper; namespace AutoMapperSamples.Configuration { namespace AutoMapperSample { public class ModelData { public string WorkName { get; set; } public DateTime WorkStartedOn { get; set; } } public class ViewModel { public string Name { get; set; } public DateTime StartedOn { get; set; } } public class TestAutoMapper { public static void Main() { var config = new MapperConfiguration(cfg => { cfg.RecognizePrefixes("Work"); cfg.CreateMap<ModelData, ViewModel>(); }); config.AssertConfigurationIsValid(); var model = new ModelData { WorkStartedOn= DateTime.Now, WorkName ="Bridge Construction" }; var viewModel = config.CreateMapper().Map<viewmodel>(model); Console.WriteLine("\nWork First Name " + viewModel.Name); Console.WriteLine("\nWork Start Date " + viewModel.StartedOn); Console.ReadKey(); } } } }
And here's the output
We can also use the MapFrom method while creating the map for specifying the source columns for achieving the outcome, but the drawback of this method is that we need to specify it for all the columns. With RecognizeXXX method we do at a global level there by avoiding a lot of redundant code.
By default Automapper ignore the word "Get" in the prefixes, so to clear this you can use the following method.
var config = new MapperConfiguration(cfg => { cfg.ClearPrefixes(); cfg.CreateMap<ModelData, ViewModel>(); });
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