加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – 使用Ninject注入AutoMapper依赖项

发布时间:2020-12-13 20:24:48 所属栏目:百科 来源:网络整理
导读:使用Ninject将AutoMapper注入ASP.NET MVC 2应用程序时遇到麻烦.我用Jimmy Bogard的帖子在 AutoMapper and StructureMap type Configuration作为指导. public class AutoMapperModule : NinjectModule{ public override void Load() { BindITypeMapFactory().
使用Ninject将AutoMapper注入ASP.NET MVC 2应用程序时遇到麻烦.我用Jimmy Bogard的帖子在 AutoMapper and StructureMap type Configuration作为指导.
public class AutoMapperModule : NinjectModule
{
    public override void Load()
    {
        Bind<ITypeMapFactory>().To<TypeMapFactory>();
        Bind<Configuration>().ToSelf().InSingletonScope().WithConstructorArgument("mapper",MapperRegistry.AllMappers);
        Bind<IConfiguration>().To<Configuration>();
        Bind<IConfigurationProvider>().To<Configuration>();
        Bind<IMappingEngine>().To<MappingEngine>();
    }
}

解析配置时,Ninject抛出异常.

Error activating IObjectMapper
No matching bindings are available,and the type is not self-bindable.
Activation path:
3) Injection of dependency IObjectMapper into parameter mappers of constructor of type Configuration

更新

现在正在使用以下绑定:

Bind<ITypeMapFactory>().To<TypeMapFactory>();
    Bind<Configuration>().ToConstant(new Configuration(Kernel.Get<ITypeMapFactory>(),MapperRegistry.AllMappers())).InSingletonScope();
    Bind<IConfiguration>().ToMethod(c => c.Kernel.Get<Configuration>());
    Bind<IConfigurationProvider>().ToMethod(c => c.Kernel.Get<Configuration>());
    Bind<IMappingEngine>().To<MappingEngine>();

我在GitHub上发布了这个模块. AutoMapper.Ninject.有关我的博客的更多信息:http://binaryspeakeasy.com/2010/09/automapper-ninject/

您可以使用最新版本(目前为2.2.0)做一个班轮.
kernel.Rebind<IMappingEngine>().ToMethod(context => Mapper.Engine);

作为一个额外的,我同意fodonnel,添加一个门面来隐藏Automapper接口是一个好主意,但是我不会直接从Automapper采取签名,除非你需要所有的功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读