带有Ninject混淆的AutoMapper [英] AutoMapper with Ninject confusion

查看:84
本文介绍了带有Ninject混淆的AutoMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For starters I'm using this module:

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

我的所有地图都有一个引导程序类

I have a bootstrapper class for all my maps

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

我有解析器可以访问数据库,并且需要注入存储库. 它按原样工作,但我不知道如何将其与单元测试和IMappingEngine一起使用.

I have resolvers that access the database and need the repositories injected. It's working as is, but I can't figure out how to get it to work with unit tests and IMappingEngine.

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

_mappingEngine.Map会引发异常,因为不存在任何映射. Mapper.Map有效.

_mappingEngine.Map throws an exception because no map exists. Mapper.Map works.

我想念什么?如何使我的引导程序与单元测试一起使用,以便解析器中的存储库可以使用假/模拟存储库?

What am I missing? How do I get my bootstrapper to work with unit tests so that the repositories in my resolvers to use the fake/mock repositories?

推荐答案

尝试更改映射的绑定.

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

这篇关于带有Ninject混淆的AutoMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆