ninject - 带有 Ninject 混淆的 AutoMapper

标签 ninject automapper

对于初学者,我正在使用这个模块:

    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>();
    }
}

我的所有 map 都有一个 Bootstrap 类
        public static void Configure(IKernel kernel)
    {
        Mapper.Initialize(map => map.ConstructServicesUsing(t => kernel.Get(t)));
    }

我有访问数据库并需要注入(inject)存储库的解析器。
它按原样工作,但我不知道如何让它与单元测试和 IMappingEngine 一起工作。
        public HomeController(IMappingEngine mappingEngine)
    {
        _mappingEngine = mappingEngine;
    }

_mappingEngine.Map 抛出异常,因为不存在 map 。 Mapper.Map 有效。

我错过了什么?如何让我的 Bootstrap 使用单元测试,以便解析器中的存储库使用假/模拟存储库?

最佳答案

尝试更改映射的绑定(bind)。

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

关于ninject - 带有 Ninject 混淆的 AutoMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10938740/

相关文章:

linq - AutoMapper ProjectTo 因 FirstOrDefault 失败

asp.net-mvc-3 - 使用 MVC 3 依赖注入(inject)和 Ninject 2.2 将全局操作过滤器绑定(bind)到区域中的所有 Controller

c# - 如何根据传递给 ASP.NET MVC 中的 Controller /操作的属性值有条件地绑定(bind)到 ninject?

c# - 如何在让 Designer 满意的同时将依赖注入(inject)应用于 UserControl View ?

c# - AutoMapper.Collection.EntityFramework 在 Persist.InsertOrUpdate 之后导致 InvalidOperationException

c# - 通过 id 获取项目并使用自动映射器将其标题映射到字符串

c# - 使用 AutoMapper 强制浅拷贝

c# - 如何正确设置 IIS 7 应用程序池标识?

c# - Ninject Factory 扩展与 Lazy<T>

c# - AutoMapper - 从 v8 升级到 v9 后不使用泛型