开发者

Ninject 2.0: Passing different parameters depending on implementation

开发者 https://www.devze.com 2023-01-15 02:41 出处:网络
I have just started to work with Ninject 2.0 with ASP.NET MVC 2. So, I have an interface IMongoRepository and class MongoRepository.

I have just started to work with Ninject 2.0 with ASP.NET MVC 2. So, I have an interface IMongoRepository and class MongoRepository.

MongoRepository receives a parameter string collection.

Depending on the collection I want to use, I pass in a different value in parameter for MongoRepository. I hope I am phrasing this correctly, but how would I map different parameters depending on the Controller I am using?

For example, in the Article controller I would call:

_articlesRepository = new MongoRepository("Articles");

and in the PageController I would call:

_pagesController = new MongoRepository("Pages");

What I would like to do is just do constructor injection and just pass in IMongoRepository. Any ideas or suggestions?

By the way, I am just learning about the IOC/DI; so, I am open to any tips from th开发者_JAVA百科e IOC ninjas! Thanks!


Try the following:

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

Assuming that the constructor's argument is called topic.

0

精彩评论

暂无评论...
验证码 换一张
取 消