温莎城堡:如何更新组件注册 [英] Castle Windsor: How can I update a components registration
问题描述
如果我已在config中定义:
If I have defined in config:
container.Register(
Component.For<X.Y.Z.IActivityService>()
.ImplementedBy<X.Y.Z.ActivityService>()
.ServiceOverrides(ServiceOverride.ForKey("Listeners").Eq(new [] { typeof(X.Y.Z.DefaultActivityListener).FullName }))
.LifeStyle.Transient
);
,我希望扩展此配置并将新项添加到Listeners数组属性中,以便最终配置有效:
and I wish to extend this configuration and add a new item to the Listeners array property such that the final configuration is effectively:
container.Register(
Component.For<X.Y.Z.IActivityService>()
.ImplementedBy<X.Y.Z.ActivityService>()
.ServiceOverrides(ServiceOverride.ForKey("Listeners").Eq(new [] { typeof(X.Y.Z.DefaultActivityListener).FullName, "MyOtherListenerID" }))
.LifeStyle.Transient
);
在首次注册组件时,我必须知道数组的内容,还是可以检索
must I know the contents of the "array" when first registering the component, or can I retrieve the component registration and add to it?
我希望使用装饰器模式实现我的配置,这样我可以构建我的容器,然后根据需要扩展它以适应不同的情况。这意味着我需要能够访问已配置的组件并将其添加到其中。
I wish to implement my config using the decorator pattern such that I can build my container, and then extend it as needed for different scenarios. This means I need to be able to access the already configured components and add to them.
考虑使用一个类 DefaultConfig
返回默认设置,然后返回更多 DecoratedConfig
类之一,这将扩展默认配置。
Was thinking of having a class DefaultConfig
which return the default setup and then one of more "DecoratedConfig
" classes, that would extend the default config.
所以我会有
IWindsorContaner c = new DecoratedConfig(new DefaultConfig()).InitialiseContainer();
DefaultConfig
将设置 ActivityService
和 DefaultActivityListener
(如示例所示)。
DefaultConfig
would set up the ActivityService
with a DefaultActivityListener
(as shown in example).
DecoratedConfig
会识别出已创建 ActivityService
并将其自己的监听器实现添加到 Listeners
ActivityService
上的code>数组。
DecoratedConfig
would recognise that ActivityService
had been created and add its own Listener implementation to the Listeners
array on ActivityService
.
谢谢。
推荐答案
订阅 Kernel.ComponentModelCreated
事件。您可以从此处更改任何组件参数。请参见此。不必拥有来做到这一点,但这很方便。
Subscribe to the Kernel.ComponentModelCreated
event. You can change any component parameter from there. See this. It doesn't have to be a facility who does this, but it's convenient.
这篇关于温莎城堡:如何更新组件注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!