Using Ninject IOC to replace a factory

為{幸葍}努か 提交于 2019-12-04 23:40:13

If I follow your question correctly, it sounds like you want to retrieve a named binding. You didn't mention what version of Ninject you are using, but based on your code snippet, I am guessing you are using Ninject 2.0. If that's the case then I would think this would suffice for your binding in your module:

Bind<ITokenHandler>().To<YourConcreteTypeHere>().Named(tokenName);

You bind as many concrete types to the same interface and differentiate them by name, and then retrieve them using the precise syntax you've specified in your question.

If I am missing something key, let me know.

One technique I've used is to Bind stuff in such a way that you can require handing in of a parameter (in the context) at the point where you want someone to select something out.

Between http://ninject.codeplex.com/wikipage?title=Providers%20and%20the%20Activation%20Context and http://ninject.codeplex.com/wikipage?title=Contextual%20Binding you should be able to Bind things in such a way that you can say Only(When.Context...) to make the selection work?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!