如何使用Autofac注入AutoMapper? [英] How to inject AutoMapper with Autofac?

查看:2759
本文介绍了如何使用Autofac注入AutoMapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是注入AutoMapper到其他层的正确方法?

我看了这个博客<一href=\"http://ofarooq.com/index.php/configuring-automapper-with-autofac-ioc-container-to-remove-dependency-on-static-mapper/\"相对=nofollow>帖子,但这code以下原因


  

类型的异常AutoMapper.AutoMapperMappingException发生在AutoMapper.dll但在用户code没有处理


在尝试映射服务层。

 列表&LT; StudentViewModel&GT;列表2 = _mapper.Map&LT;名单,LT; StudentViewModel&GT;&GT;(名单);

我AutoFac配置如下图所示:

 公共静态类DependencyRegistration
{
    公共静态无效配置()
    {
        VAR建设者=新ContainerBuilder();        builder.RegisterControllers(typeof运算(MvcApplication).Assembly);
        builder.RegisterType&LT; TypeMapFactory&GT;()为&lt;&ITypeMapFactory GT;();
        builder.RegisterType<ConfigurationStore>().As<ConfigurationStore>().WithParameter(\"mappers\", MapperRegistry.Mappers).SingleInstance();
        builder.Register((CTX,T)=&GT; ctx.Resolve<ConfigurationStore>()).As<IConfiguration>().As<IConfigurationProvider>();
        builder.RegisterType&LT;&引擎(MappingEngine)GT;()为&lt;&IMappingEngine GT;();        // ...
        变种容器= builder.Build();
        DependencyResolver.SetResolver(新AutofacDependencyResolver(容器));
    }
}


解决方案

看来你需要使用在容器里创建的地图中注册的 IConfiguration 对象像这样的:

  VAR配置= container.Resolve&LT; IConfiguration&GT;();
configuration.CreateMap&LT;学生,StudentViewModel&GT;();

我认为你应该在你的应用程序开始时这样做。

下面是一个更好的办法(IMO)配置中的配置法的事情:

 公共静态无效配置()
{
    VAR configuration_store =新ConfigurationStore(新TypeMapFactory(),MapperRegistry.Mappers);    VAR mapping_engine =新引擎(MappingEngine)(configuration_store);    configuration_store.CreateMap&LT;学生,StudentViewModel&GT;();    VAR建设者=新ContainerBuilder();    builder.RegisterInstance(mapping_engine)。至于&LT; IMappingEngine&GT;();    // ...
    变种容器= builder.Build();
    DependencyResolver.SetResolver(新AutofacDependencyResolver(容器));
}

我假设在最后一个例子,你的类只需要 IMappingEngine 访问(而不是 IConfiguration ) ,因为你应该已经安装在配置方法,所有映射(或在应用程序启动一些其他的配置方法)。

What is the proper way to inject AutoMapper to other layers?

I read this blog post , but this code cause exception below

An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code

when try mapping in service layer.

List<StudentViewModel> list2 = _mapper.Map<List<StudentViewModel>>(list);

My AutoFac configuration like below:

public static class DependencyRegistration
{
    public static void Config()
    {
        var builder = new ContainerBuilder();

        builder.RegisterControllers(typeof(MvcApplication).Assembly);


        builder.RegisterType<TypeMapFactory>().As<ITypeMapFactory>();
        builder.RegisterType<ConfigurationStore>().As<ConfigurationStore>().WithParameter("mappers", MapperRegistry.Mappers).SingleInstance();
        builder.Register((ctx, t) => ctx.Resolve<ConfigurationStore>()).As<IConfiguration>().As<IConfigurationProvider>();
        builder.RegisterType<MappingEngine>().As<IMappingEngine>();

        //...
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }
}

解决方案

It seems that you need to use the IConfiguration object that is registered in the container to create the maps like this:

var configuration = container.Resolve<IConfiguration>();
configuration.CreateMap<Student, StudentViewModel>();

I think that you should be doing this at the start of your application.

Here is a better way (IMO) to configure things in the Config method:

public static void Config()
{
    var configuration_store = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);

    var mapping_engine = new MappingEngine(configuration_store);

    configuration_store.CreateMap<Student, StudentViewModel>();

    var builder = new ContainerBuilder();

    builder.RegisterInstance(mapping_engine).As<IMappingEngine>();

    //...
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

I am assuming in the last example, that your classes need access only to IMappingEngine (and not IConfiguration), since your should already setup all mappings in the Config method (or some other configuration method at application startup).

这篇关于如何使用Autofac注入AutoMapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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