2012-09-21 57 views
8

我以前使用Autofac,但现在我想给SimpleInjector一试。我的问题是,在叫我的方法中的mappingEngine我得到以下错误:简单的喷油器注册IMappingEngine(AutoMapper)

Missing type map configuration or unsupported mapping.

Mapping types: Something -> SomethingDto Destination path: IEnumerable`1[0]

Source value:

_mappingEngine.Map<IEnumerable<SomethingDto>>(IEnumerableOfSomething); 

^-- doesn't work 

Mapper.Map<IEnumerable<SomethingDto>>(IEnumerableOfSomething); 

^-- works (That's not what I want) 

Mapper.Map不是我想要的。林注册Automapper在此基础上在这里:

Replace Ninject with Simple Injector

container.Register<ITypeMapFactory, TypeMapFactory>(); 
container.RegisterAll<IObjectMapper>(
    MapperRegistry.AllMappers()); 
container.RegisterSingle<ConfigurationStore>(); 
container.Register<IConfiguration>(() => 
    container.GetInstance<ConfigurationStore>()); 
container.Register<IConfigurationProvider>(() => 
    container.GetInstance<ConfigurationStore>()); 
container.Register<IMappingEngine, MappingEngine>(); 

Mapper.Initialize(x => 
      { 
       var profiles = container.GetAllInstances<Profile>(); 

       foreach (var profile in profiles) 
       { 
        x.AddProfile(profile); 
       } 
      }); 

     Mapper.AssertConfigurationIsValid(); 

我的问题IST,我怎么在SimpleInjector注册IMappingEngine和正确添加我的档案?

在此先感谢!

迎接mtrax

回答

8

解决了! :-)

在启动MvcControllerFactory后,我注册了我的配置文件太晚了。我希望我的伪示例可以帮助你防止这样的错误。

// SimpleInjector 
var container = new Container(); 

// AutoMapper registration 
container.Register<ITypeMapFactory, TypeMapFactory>(); 
container.RegisterCollection(MapperRegistry.Mappers); 
container.RegisterSingleton<ConfigurationStore>(); 
container.Register<IConfiguration>(container.GetInstance<ConfigurationStore>); 
container.Register<IConfigurationProvider>(container.GetInstance<ConfigurationStore>); 
container.RegisterSingleton(() => Mapper.Engine); 

// AutoMapper Profiles registration 
container.RegisterCollection<Profile>(new MappingAProfile(), 
             new MappingBProfile(), 
             new MappingCProfile()); 

// Adding AutoMapper profiles 
Mapper.Initialize(x => 
    { 
     var profiles = container.GetAllInstances<Profile>(); 

     foreach (var profile in profiles) 
     { 
      x.AddProfile(profile); 
     } 
    }); 

Mapper.AssertConfigurationIsValid(); 

container.Verify(); 

container.RegisterAsMvcControllerFactory(); 

* RegisterAsMvcControllerFactory()找到:Simple Injector MVC Integration Guide

相关问题