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

Add a Comment