迁移到AutoMapper 4.2/5.0时,应该在依赖项注入容器中存储IMapper实例还是MapperConfiguration实例? [英] When migrating to AutoMapper 4.2/5.0, should I store an IMapper instance or the MapperConfiguration instance in my dependency injection container?

查看:75
本文介绍了迁移到AutoMapper 4.2/5.0时,应该在依赖项注入容器中存储IMapper实例还是MapperConfiguration实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迁移到较新的配置.我正在查看 AutoMapper GitHub Wiki 上的示例,并且对如何完成感到有些困惑配置. Wiki在一个地方说,您可以将MapperConfiguration实例存储在D.I.中.容器(或静态存储),但下一段说明您可以静态存储Mapper实例.简而言之,我不确定是否应该这样做

I am migrating to the newer configuration of AutoMapper. I was looking at examples on the AutoMapper GitHub Wiki and am a little confused on how to complete the configuration. The Wiki says in one place you can store the MapperConfiguration instance in your D.I. container (or store it statically), but the next paragraph says you can store the Mapper instance statically. In a nutshell, I am not sure if I should be doing

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<Foo, Bar>().ReverseMap(); //creates a bi-directional mapping between Foo & Bar
    cfg.AddProfile<FooProfile>(); //suppose FooProfile creates mappings...
});

然后使用D.I.诸如Unity之类的容器,用于将实例存储为此类...

then using a D.I. container such as Unity to store that instance as such...

container.RegisterInstance<MapperConfiguration>(config);

以后我可以在哪里使用此实例执行映射...

Where I could later use this instance to perform mappings...

public void CreateMapping(MapperConfiguration config, Bar bar)
{
     Foo foo = config.CreateMapper().Map(bar);
     //do stuff with foo
}

或者,我应该存储MapperConfiguration创建的IMapper实例

or, should I be storing the IMapper instance the MapperConfiguration makes

container.RegisterInstance<IMapper>(config.CreateMapper());

其用法如下

public void CreateMapping(IMapper mapper, Bar bar)
{
     Foo foo = mapper.Map(bar);
     //do stuff with foo
}

在初始配置调用Map方法之后,我将在应用程序中做的所有事情.我将不需要修改配置.我应该将IMapper实例还是MapperConfiguration实例存储在依赖项注入容器中?

All I will be doing in my application after the initial configuration is calling the Map method. I will not need to modify the configuration. Should I store the IMapper instance or the MapperConfiguration instance in my dependency injection container?

更新:我最终在我的D.I.中注册了IMapper.容器.就我而言,Unity.

Update: I ended up with registering IMapper with my D.I. container. In my case, Unity.

推荐答案

我不明白为什么不能同时存储两者.在某些情况下,我需要注入一个或另一个或两者,因为我将MapperConfiguration用于LINQ投影,而将IMapper用于进行映射本身.我的IoC注册看起来像这样:

I don't see why you couldn't store both. I have cases where I need to inject one or the other or both, as I use MapperConfiguration for LINQ projections and IMapper for doing the mapping itself. My IoC registrations look like this:

public static Container RegisterAutoMapper(this Container container)
{
    var profiles = typeof(AutoMapperRegistry).Assembly.GetTypes().Where(t => typeof(Profile).IsAssignableFrom(t)).Select(t => (Profile)Activator.CreateInstance(t));

    var config = new MapperConfiguration(cfg =>
    {
        foreach (var profile in profiles)
        {
            cfg.AddProfile(profile);
        }
    });

    container.RegisterSingleton<MapperConfiguration>(() => config);
    container.RegisterSingleton<IMapper>(() => container.GetInstance<MapperConfiguration>().CreateMapper());

    return container;
}

}

由于IMapper对象可以由MapperConfiguration创建,因此我的IoC正在从MapperConfiguration的当前注册中生成IMapper实例.

As the IMapper object can be created by the MapperConfiguration, my IoC is spawning an IMapper instance from the current registration of MapperConfiguration.

这篇关于迁移到AutoMapper 4.2/5.0时,应该在依赖项注入容器中存储IMapper实例还是MapperConfiguration实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆