Ninject GetAll其中模块= foo [英] Ninject GetAll Where Module = foo
问题描述
是否可以执行以下操作:
Is it possible to do something like this:
var foos = Kernel.where(c=>c.module == myModule).GetAll<foo>;
那怎么办呢?
如果(!可能)
使用GetAll()方法时,如何区分到同一接口的绑定?
我知道WithParameter
,WithMetadata
,WithConstructorArgument
和WithPropertyValue
;我应该使用哪一个?为什么?
If(!possible)
How can you discriminate between bindings to the same interface when using the GetAll() method?
I am aware of WithParameter
, WithMetadata
, WithConstructorArgument
and WithPropertyValue
; Which one should i use and why?
欢呼
更多信息
我需要这个,因为我正在创建一个插件框架.在路由期间,插件框架需要能够确定要查看哪个控制器.如果各个插件中有多个家庭控制器,它将无法按预期工作.
我需要能够区分哪个插件注册了哪个IController实现,以便控制器工厂知道将请求发送到特定插件中的控制器.
I need this because I am creating a plugin framework. During the routing the plugin framework needs to be able to determine which controller's to look at. If there are multiple home controllers across plugins it wont work as expected.
I need to be able to differentiate which plugin registered which IController implementation so that the controller factory knows to send the request to a controller in a specific plugin.
推荐答案
我最终将元数据附加到每一个:
I ended up attaching metadata to each one:
Bind<IController>().To<MyController>()
.Named(implementation.GetType().Name.Replace("Controller", string.Empty))
.WithMetadata("Plugin", PluginName);`
然后像这样路由我的插件:
Then routing my plugins like so:
routes.MapRoute(
name: "Plugin1",
url: "Plugin1/{controller}/{action}/{id}",
defaults: new { Plugin = PluginName, controller = "Home", action = "Index", id = UrlParameter.Optional }
);
在控制器工厂的GetControllerType方法中
And in the controller factory GetControllerType method
object requestedModule;
if (requestContext.RouteData.Values.TryGetValue("Plugin", out requestedPlugin))
controller = kernel.GetAll<IController>(c => c.Name == controllerName && c.Get<string>("module", string.Empty) == (string)requestedPlugin).SingleOrDefault();
这篇关于Ninject GetAll其中模块= foo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!