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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With