Ninject 2.0: passing various parameters depending on the implementation

I just started working with Ninject 2.0 using ASP.NET MVC 2. So, I have the IMongoRepository interface and the MongoRepository class.

MongoRepository gets a collection of parameter strings.

Depending on the collection I want to use, I pass a different value in the parameter for MongoRepository. I hope that I formulate this correctly, but how could I display various parameters depending on the controller I use?

For example, in an article controller, I would call:

_articlesRepository = new MongoRepository("Articles");

and in PageController I would call:

_pagesController = new MongoRepository("Pages");

What I would like to do is just make an injection to the constructor and just go through the IMongoRepository. Any ideas or suggestions?

By the way, I'm just learning about IOC / DI; therefore, I am open to any advice from the IOC ninja! Thank!

+3
1

:

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

, topic.

+11

Source: https://habr.com/ru/post/1762926/


All Articles