2012-06-07 34 views
3

對於我使用這個模塊首發:AutoMapper與Ninject混亂

public class AutoMapperModule : NinjectModule 
{ 
    public override void Load() 
    { 
     Bind<ITypeMapFactory>().To<TypeMapFactory>(); 
     foreach (var mapper in MapperRegistry.AllMappers()) 
     { 
      Bind<IObjectMapper>().ToConstant(mapper); 
     } 

     Bind<AutoMapper.ConfigurationStore>().ToSelf().InSingletonScope().WithConstructorArgument("mappers", ctx => ctx.Kernel.GetAll<IObjectMapper>()); 
     Bind<IConfiguration>().ToMethod(ctx => ctx.Kernel.Get<AutoMapper.ConfigurationStore>()); 
     Bind<IConfigurationProvider>().ToMethod(ctx => ctx.Kernel.Get<AutoMapper.ConfigurationStore>()); 
     Bind<IMappingEngine>().To<MappingEngine>(); 
    } 
} 

我有我的所有地圖引導程序類

 public static void Configure(IKernel kernel) 
    { 
     Mapper.Initialize(map => map.ConstructServicesUsing(t => kernel.Get(t))); 
    } 

我有一個訪問數據庫和需要的庫解析器注射。 它的工作原理,但我不知道如何讓它與單元測試和IMappingEngine一起工作。

 public HomeController(IMappingEngine mappingEngine) 
    { 
     _mappingEngine = mappingEngine; 
    } 

_mappingEngine.Map拋出異常,因爲沒有映射存在。 Mapper.Map可以工作。

我錯過了什麼?我如何讓自舉程序使用單元測試,以便我的解析程序中的存儲庫使用假/模擬存儲庫?

回答

1

嘗試更改映射的綁定。

Bind<IMappingEngine>().ToMethod(ctx => Mapper.Engine);