Autofac 3和Automapper [英] Autofac 3 and Automapper

查看:70
本文介绍了Autofac 3和Automapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有人知道使用Autofac设置Automapper的综合指南.我对这两者都是新手,但是我玩过静态Mapper类,但是我希望能够模拟和注入IMappingEngine并创建一个设置所有映射的配置.到目前为止,我看过的所有指南都没有真正解释正在发生的事情,我也无法完全解决.另外,我使用的是Autofac 3.0,它在ContainerBuilder方法中似乎有一些区别,这无济于事(我使用它的原因是Autofac.mvc4依赖于它).

Does anyone know of a comprehensive guide to setting up Automapper with Autofac. I'm new to both but I have played around with the static Mapper class however I want to be able to mock and inject IMappingEngine and create a configuration that sets up all my mappings. All the guides I have looked at so far don't really explain what is going on and I can't quite work it out. Also I am using Autofac 3.0 which seems to have some differences in the ContainerBuilder methods which doesn't help (the reason I'm using it is that Autofac.mvc4 depends on it).

更新:

好的,最简单的解决方案似乎可以很好地工作,但是我在互联网上的任何地方都没有看到它,也许出于一个我不知道的充分理由?最简单的操作是将静态Mapper.Engine注册为IMappingEngine,并且仍然首先使用静态Mapper.CreateMap进行配置.

OK, the simplest solution seems to work well enough, however I had not seen it anywhere on the internet and that maybe for a good reason that I don't know? The simplest thing to do is just to Register the static Mapper.Engine as IMappingEngine and still use the static Mapper.CreateMap to configure in the first place.

var builder = new ContainerBuilder();
builder.Register<IMappingEngine>(c => Mapper.Engine);

现在,Autofac可以将IMappingEngine注入到您的构造函数中.这确实意味着Mapper将处理IMappingEngine单例而不是Autofac,而Autofac只是充当它的包装器.我希望Autofac处理IMappingEngine实例,但不确定如何?

Now Autofac can inject the IMappingEngine into your constructors. This does mean that Mapper will handle the IMappingEngine singleton rather than Autofac and Autofac is just acting as a wrapper for it. I would like Autofac to handle the IMappingEngine instance but I'm not sure how?

推荐答案

您简单的解决方案就可以了,只要您不想在单元测试中模拟映射器,或者不想为嵌套生存期作用域创建具有修改配置的映射器(后一个)对我来说看起来有些怪异,但谁知道.)

Your simple solution is OK provided that you don't want to mock the mapper in unit tests or create mappers with modified configurations for nested lifetime scopes (the latter one looks a bit weird to me, but who knows).

如果需要,您可以从 并注册这样的组件:

If you need that, you can pick up some pieces of code from the Mapper class and register components like this:

builder.Register(ctx => new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers()))
       .AsImplementedInterfaces()
       .SingleInstance();

builder.RegisterType<MappingEngine>()
       .As<IMappingEngine>();

我不确定您是否真的需要将IMappingEngine单身.每个依赖项创建起来应该是轻量级的.

I'm not sure if you really need to make IMappingEngine a singleton. It should be quite lightweight to create per dependency.

现在您可以像这样使用它:

Now you can use it like this:

// in a bootstrapper:
var mapperConfig = ctx.Resolve<IConfiguration>();
mapperConfig.CreateMap<A, B>();

// later on:
public class X{
    IMappingEngine _mapper;

    public X(IMappingEngine mapper){
        _mapper = mapper;
    }

    public B DoSmth(){
        return _mapper.Map<B>(new A());
    }
}

您还可以像这样设置自动配置文件注册:

You can also set up automatic profiles registration like this:

builder.Register(ctx => new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers()))
       .AsImplementedInterfaces()
       .SingleInstance()
       .OnActivating(x => {
           foreach (var profile in x.Context.Resolve<IEnumerable<Profile>>()){
               x.Instance.AddProfile(profile);
           }
       });

然后只需在Autofac配置或模块中的任何位置注册Profile实现,即可将其连接到配置.

Then just register a Profile implementation anywhere in Autofac configuration or in a module to get it hooked up to the configuration.

这篇关于Autofac 3和Automapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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