Ninject 2.0:根据实现传递不同的参数

问题描述:

我刚刚开始使用ASP.NET MVC 2的Ninject 2.0。 所以,我有一个接口IMongoRepository和类MongoRepository。Ninject 2.0:根据实现传递不同的参数

MongoRepository接收参数字符串集合。

根据我想要使用的集合,我为MongoRepository传入一个不同的参数值。 我希望我是正确的措辞,但我将如何根据我使用的控制器映射不同的参数?

例如,在第二十控制器我会打电话:

_articlesRepository = new MongoRepository("Articles"); 

,并在的PageController我会打电话:

_pagesController = new MongoRepository("Pages"); 

我想什么做的只是做构造函数注入和公正传入IMongoRepository。 任何想法或建议吗?

顺便说一下,我只是了解国际奥委会/ DI;所以,我愿意接受国际奥委会忍者的任何提示! 谢谢!

+0

这个链接似乎很有希望! http://stackoverflow.com/questions/3313940/ninject-to-bind-on-different-controllers – Abe 2010-09-02 22:51:48

尝试以下操作:

Bind<IMongoRepository>().To<MongoRepository>().WhenInjectedInto<ArticleController>().WithConstructorArgument("topic", "Article"); 
Bind<IMongoRepository>().To<MongoRepository>().WhenInjectedInto<PagesController>().WithConstructorArgument("topic", "Pages"); 

假设构造函数的参数被称为topic