使用 Ninject 注入 AutoMapper 依赖项
我在使用 Ninject 将 AutoMapper 注入 ASP.NET MVC 2 应用程序时遇到问题。我使用了 Jimmy Bogard 在 AutoMapper 和 StructureMap 上的帖子输入配置作为指导。
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 在解析 Configuration
时抛出异常。
激活 IObjectMapper 时出错 没有可用的匹配绑定,并且该类型不可自绑定。 激活路径:
3)将依赖项IObjectMapper注入到Configuration类型的构造函数的参数映射器中
Update
类型的构造函数的参数映射器中这现在使用以下绑定进行工作:
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/
I am having trouble injecting AutoMapper into an ASP.NET MVC 2 application using Ninject. I used Jimmy Bogard's post on AutoMapper and StructureMap type Configuration as a guide.
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 throws an exception when resolving Configuration
.
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
Update
This is now working using the following binding:
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>();
I published the module on GitHub. AutoMapper.Ninject. More information on my blog: http://binaryspeakeasy.com/2010/09/automapper-ninject/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用最新版本(当前为 2.2.0)执行此操作。
另外,我确实同意 fodonnel 的观点,添加一个外观来隐藏 Automapper 接口是一个好主意,但是我不会直接从 Automapper 获取签名,除非您需要所有这些功能。
You can do this is a one liner using the latest version (currently 2.2.0).
As an extra, I do agree with fodonnel, adding a facade to hide the Automapper interface is a good idea, however I wouldn't take the signatures directly from Automapper, unless you need all that functionality.
引入映射外观也可能是个好主意。不要在代码中传递 IMappingEngine,而是创建 IObjectMapper 接口。我使用的接口包含直接从自动映射器代码中获取的方法签名。
您的配置仍然依赖于自动映射器。
我写的一篇博客文章在这里: http:// /fodonnel.wordpress.com/2010/09/20/an-object-mapper-facade/
It might also be a good idea to introduce a mapping facade. Instead of passing IMappingEngine through out your code create an IObjectMapper interface. The interface I use contains method signatures taken directly out of automappers code.
Your configuration is still going to be automapper dependent.
A blog post I wrote on it is here: http://fodonnel.wordpress.com/2010/09/20/an-object-mapper-facade/
我让它工作了,但创建 Configuration 类的实例感觉不太干净。任何进一步清理它的建议。
I got it working but it doesn't feel very clean creating an instance of the Configuration class. Any suggestions to clean it up further.