什么是注入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?
发布评论