使用Ninject混淆的AutoMapper

3

首先,我正在使用这个模块:

    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个回答

1
尝试更改映射的绑定。
Bind<IMappingEngine>().ToMethod(ctx => Mapper.Engine);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接