AutoMapper,远离过时的静态API

时间:2016-03-21 13:33:27

标签: c# automapper

我正在使用现有的解决方案。该解决方案使用Windsor IoC。我有一个AutomapperMappings.cs类看起来像这样:

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>();
    }
}

在我的Global.asax中,我有以下内容:

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

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

以上是给我一个警告,说我应该放弃静态API。所以我用Google搜索并做了一些阅读,建议我将AutomapperMappings.cs更改为:

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>();
        });
    }
}

这一切都很好,但变量var config实际上并没有在任何地方使用,所以我确定我需要做更多的事情,但我不知道我需要改变什么以及在哪里。

2 个答案:

答案 0 :(得分:5)

有教程"Migrating from static API"

您需要创建mapper对象并将其注册到IoC容器:

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));
    }
}

现在,您可以将映射器注入需要映射实体的类:

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);
    }
}

通过创建IMapper接口的模拟对象,此迁移将帮助您使代码更易于测试。

答案 1 :(得分:0)

我会在这里查看Jimmy Bogards(Automapper创建者)帖子:

https://lostechies.com/jimmybogard/2016/01/21/removing-the-static-api-from-automapper/

IMapper mapper = config.CreateMapper();

配置可用于返回IMappper接口,可以更方便地用于测试目的,创建新的映射等。