使用Ninject国际奥委会更换工厂 [英] Using Ninject IOC to replace a factory

查看:162
本文介绍了使用Ninject国际奥委会更换工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解析器内的工厂方法。从本质上讲,我加载一个令牌我仰望的处理程序令牌,或者通过对默认处理程序下降。我实现了这是一个开关和一个词典<字符串类型> 但是这两种方法需要我来存储映射其他地方比处理程序类。

I've got a factory method inside a parser. Essentially as I load a token I look up the handler for that token, or drop through to the default handler. I've implemented this as a switch and as a Dictionary<string,Type> but both approaches require me to store the mapping somewhere else than the handler class.

我们正在使用Ninject为国际奥委会等我我也可以用做

We are using Ninject for IOC and so I've realized I can also do it using

kernel.Get<ITokenHandler>(tokenName); 



但是,这并不救我存储在什么记号处理程序的信息,可以处理在2个地点。有没有一种方法,我可以装点处理程序,以便这一被自动映射?

but that doesn't save me storing the information on what token the handler can deal with in 2 locations. Is there a way I can decorate the handler so this gets mapped automatically?

推荐答案

如果我正确地按照你的问题,这听起来像要检索名为绑定。你没有提到你使用的是什么版本的Ninject,但基于您的代码段,我猜你正在使用Ninject 2.0。如果是这样的话,那么我认为这将足以为你的模块中绑定:

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.

这篇关于使用Ninject国际奥委会更换工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆