AutoMapper, moving away from the Obsolete Static API

Viewed 6185

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.

2 Answers
Related