开发者

Castle Windsor: how to overwrite a component registration via xml configuration

开发者 https://www.devze.com 2023-04-04 18:38 出处:网络
I\'m trying to overwrite a component registration (done via xml) with another one (in another xml, loaded via code using container.Install(Configuration.FromXmlFile(\"...\")) that has the same compone

I'm trying to overwrite a component registration (done via xml) with another one (in another xml, loaded via code using container.Install(Configuration.FromXmlFile("...")) that has the same component id, but i receive an error saying:

"Component COMP_ID could not be registered. There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name."

The thing is, i in fact actually would like to modify the existing component, to be able to define basic wiring in a basic castle.xml file and then overwrite it, in another custom.xml file, loaded after the basic one.

I'm trying to use the same component id because a tought it would have rewritten the previous registration.

The reality is that i would like to change wich implementation is used to respond to a service, the component id is just a try that i've done. Since Castle has a policy of first-registration-wins, it would be enough for example to switch to a last-registration-wins, for example.

Concrete example

Let's use the example of an ILogger interface.

The basic castle.xml (loaded always) would be like this:

<component id="logger" service="ILogger" type="NullLogger" ></component>

The castle.dev.xml file (loaded only in the dev ambient, after the defaul开发者_如何学编程t one) would look like this:

<component id="logger" service="ILogger" type="AspNetTraceLogger" ></component>

The castle.prod.xml file (loaded only in production ambient, after the default one, instead of the dev one) would look like this:

<component id="logger" service="ILogger" type="SqlServerLogger" ></component>

Now: for each of these configurations, I would like to say "respond to ILogger requests with this impl", so I need to have only one of them to respond to requests for ILogger at each time.

Anyone have any idea?

update

I'll add: as of now i'm using a, shall we say, "reverse overwriting" technique, where i load the overwriting files "first", before the corresponding "default" ones. In this way, since the policy is first-wins, i can declare them as overwrites, even though internally they are declared before. The problem with this approach is when i have an interface that i know will be implemented more than once (say, IWhateverListener): in this case i cannot remove the other ones already registered, and cannot use the first-wins solution because later on i'll use a ResolveAll() and the "default ones" will be used too.


Don't try to register several components with the same ID, or try to hack around it.

If you must use XML, take a look at defines and ifs.

Otherwise just use different installers, one for each environment, and at application startup register the one you want

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号