Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use the key parameter when calling AddComponent on WindsorContainer?

The AddComponent method on the IWindsorContainer interface has several overloads, for example:

WindsorContainer.AddComponent<I,T>()

and

WindsorContainer.AddComponent<I,T>(string key)

What's the use of the key parameter and why should I use it?

like image 834
Gerrie Schenck Avatar asked Nov 24 '25 06:11

Gerrie Schenck


1 Answers

You would use the key parameter if you were registered multiple implementations of the same interface. That way you can later retrieve a specific one. For example, I may have multiple versions of IHandler.

container.AddComponent<IHandler, FileHandler>("handlers.file");
container.AddComponent<IHandler, HttpHandler>("handlers.http");
//I can retrieve the first one like this (or something like this).
IHandler fileHandler = container.Resolve<IHandler>();
//I can retrieve the http handler like this 
IHandler httpHandler = container.Resolve<IHandler>("handlers.http");

In addition, when you register a component without a key, I believe it's type is used as the key.

container.AddComponent<IHandler, FileHandler>();

I believe this is registered with a key of "{Namespace}.IHandler". So it could actually be retrieved later using the automatic key as well.

Hope this helps.

like image 69
Craig Wilson Avatar answered Nov 26 '25 19:11

Craig Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!