AutoMapper 5.2 how to configure

You can use the static mapper api as outlined here.

For example, somewhere in your application, probably during startup you would configure the static (global) mapper using something like:

AutoMapper.Mapper.Initialize(cfg => { 
   cfg.CreateMap<Type1, Type2>(); 
   /* etc */
});

Then, any time you need to use your "globally" configured mapper, you access it via the static Mapper property (which is an IMapper):

Type1 objectOfType1 = new Type1();
var result = AutoMapper.Mapper.Map<Type2>(objectOfType1);

You then have one mapper that has been configured for all the types/configurations/profiles you provide for the duration of your application without needing to configure individual mapper instances.

In short, you configure it once (perhaps at application startup). The static mapper instance (the IMapper) is then available anywhere throughout your application by accessing it via AutoMapper.Mapper.

Access via this static property is what you refer to as "globally" in your comments. Anywhere you need it just use AutoMapper.Mapper.Map(...) So long as you've called Initialize once first.

Note that if you call Initialize more than once on the static instance, each subsequent call overwrites the existing configuration.

WARNING In a previous release of AutoMapper, the static mapper was removed. It was later added back in and I don't know if they guarantee that it will remain in future versions. The recommendation is to use your own configured instances of a mapper. You can store it in a static property somewhere if you need it. Otherwise you can look into profiles, etc for easy ways to configure your mapper so that having your own instance isn't necessarily a "hassle".


Set this in your StartupConfig or StartUp file.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
       // Web API configuration and services    
        .....

        MappingDTOModelToModel.Configure();
    }
}

Configuration of Mappings,

public static class MappingDTOModelToModel
{       
     private static void Configure()
     {
         Mapper.Initialize(cfg =>
         {
             cfg.CreateMap<R_Logo, LogoDto>()
                 .ForMember(x => x.ID,
                            m => m.MapFrom(a => a.ID))
                 .ForMember(x => x.FirstName,
                            m => m.MapFrom(a => a.FirstName)).ReverseMap();                    
         }
     }
 }

Calling it in a method,

public class MyService
{
    public void MyMethod(var model)
    {
        var myModel = Mapper.Map<LogoDto, R_Logo>(model);  
    }
}

Hope this helps,


Here is the steps to configure the automapper in asp.net core mvc.

1. Create the mapping profile class which extends from Profile

 public class ClientMappingProfile : Profile
 {
     public ClientMappingProfile ()
     {
         CreateMap<R_Logo, LogoDto>().ReverseMap();
     }
 }

2. Create the AutoMapper Configuration Class and add your mapping profile class here.

public class AutoMapperConfiguration
{
   public MapperConfiguration Configure()
   {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<ClientMappingProfile>();
        });
        return config;
    }
}

3. How we can use it.

       var config = new AutoMapperConfiguration().Configure();
       var iMapper = config.CreateMapper();

       var dest = iMapper.Map<R_Logo, LogoDto>(logo);

Tags:

C#

Automapper