如何使用Autofac注入AutoMapper?

编程入门 行业动态 更新时间:2024-10-27 10:23:41
本文介绍了如何使用Autofac注入AutoMapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我看了这个博客帖子,但这code以下原因  

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

在尝试映射服务层。

列表< StudentViewModel>列表2 = _mapper.Map<名单,LT; StudentViewModel>>(名单);

我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?

本文发布于:2023-11-05 21:14:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Autofac   AutoMapper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!