Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper, moving away from the Obsolete Static API

Tags:

c#

automapper

I am working in an existing solution. The solution uses the Windsor IoC. I have an AutomapperMappings.cs class that looks like this:

public class AutoMapperMappings
{
    public static void Configure()
    {
        AutoMapper.Mapper.Configuration
        .CreateMap<LatestUpdateModel, LatestUpdate>();

        AutoMapper.Mapper.Configuration
        .CreateMap<LatestUpdate, LatestUpdateModel>();

        AutoMapper.Mapper.Configuration
        .CreateMap<DownloadLinkModel, DownloadLink>();

        AutoMapper.Mapper.Configuration
        .CreateMap<DownloadLink, DownloadLinkModel>();

        AutoMapper.Mapper.Configuration
        .CreateMap<NavigationElementModel, NavigationElement>();

        AutoMapper.Mapper.Configuration
        .CreateMap<NavigationElement, NavigationElementModel>();

        AutoMapper.Mapper.Configuration
        .CreateMap<Promobox, PromoboxModel>();

        AutoMapper.Mapper.Configuration
        .CreateMap<PromoboxModel, Promobox>();
    }
}

In my Global.asax, I had the following:

protected void Application_Start(object sender, EventArgs e)
{
    IoCContainer();
    ConfigureAutoMapperMappings();
}

protected virtual void ConfigureAutoMapperMappings()
{
    AutoMapperMappings.Configure();
}

The above is giving me a warning saying that I should move away from the static API. So I Googled around and did some reading that suggested I change my AutomapperMappings.cs to this:

public class AutoMapperMappings
{
    public static void Configure()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<LatestUpdateModel, LatestUpdate>();
            cfg.CreateMap<LatestUpdate, LatestUpdateModel>();
            cfg.CreateMap<DownloadLinkModel, DownloadLink>();
            cfg.CreateMap<DownloadLink, DownloadLinkModel>();
            cfg.CreateMap<NavigationElementModel, NavigationElement>();
            cfg.CreateMap<NavigationElement, NavigationElementModel>();
            cfg.CreateMap<Promobox, PromoboxModel>();
            cfg.CreateMap<PromoboxModel, Promobox>();
        });
    }
}

That is all fine, but the variable var config isn't actually used anywhere, so I'm sure I need to do some more stuff, but I don't know what I need to change and where.

like image 411
J86 Avatar asked Mar 21 '16 13:03

J86


People also ask

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

Is AutoMapper faster than manual mapping?

Automapper is considerably faster when mapping a List<T> of objects on . NET Core (It's still slower on full . NET Framework).

Does AutoMapper affect performance?

AutoMapper may result in performance costs both at startup (when mapping configurations are set up) and during processing (when actually mapping happens), and these costs might not be negligible for larger projects.

How does AutoMapper work internally?

How AutoMapper works? AutoMapper internally uses a great concept of programming called Reflection. Reflection in C# is used to retrieve metadata on types at runtime. With the help of Reflection, we can dynamically get a type of existing objects and invoke its methods or access its fields and properties.


1 Answers

There is the tutorial "Migrating from static API".

You need to create mapper object and register it to IoC container:

public class AutoMapperMappings
{
    public static void Configure(IWindsorContainer container)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<LatestUpdateModel, LatestUpdate>();
            ...
        });

        var mapper = config.CreateMapper();

        // register your mapper here.
        container.Register(Component.For<IMapper>().Instance(mapper));
    }
}

Now you can inject your mapper to the classes that need map entities:

public class ExampleClass 
{
    private readonly IMapper _mapper;
    public ExampleClass(IMapper mapper)
    {
        _mapper = mapper;
    }

    public void DoWork()
    {
        var model = new LatestUpdateModel();
        ...
        var update = mapper.Map<LatestUpdateModel, LatestUpdate>(model);
    }
}

This migration will helps you to make your code more testable by creating mock object to IMapper interface.

like image 169
Vadim Martynov Avatar answered Oct 26 '22 22:10

Vadim Martynov