views:

271

answers:

3

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?

+3  A: 

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.

Craig Wilson
+1  A: 

Finally found it somewhere in the documentation.

About halfway on this page they say

Please note that more than one implementation for the same service can be added. In this case, when you request a component by the service, the first component registered for that service will be returned. To obtain the other implementations for the same service you must use the key.

Gerrie Schenck
+1  A: 

How to choose implementation using constructor injection? I've found how to do it using xml configuration: http://blog.bittercoder.com/PermaLink,guid,6ce3f22e-763e-445c-9b96-1ca8e6116c34.aspx How to configure it from code ?

Please create a new question instead of posting as an answer
Mauricio Scheffer