Inject multiple service implementations with Castle Windsor - castle-windsor

I would like to have Windsor inject multiple implementations of a service to a constructor.
My scenario is that I have multiple search providers that implement a common interface. I would like to register each of these with windsor and then inject them into my search service so it can query each on in turn.
Is such a thing possible with Windsor? Is there a better approach than injecting multiple implementations into the constructor?

See Inversion of Control and Dependency Injection with Castle Windsor Container - Part II at DotNetSlackers. It shows how to pass an array of the same service interface to an object.

Related

Castle Windsor - not sure where to release transients created using "container.Resolve"

A little background relevant to my problem:- I'm learning to use SignalR, which is a client-server RPC framework. On the server-side you write one or more "hub" classes which expose methods that can be called remotely, a bit like a web service. When a message arrives, SignalR instantiates the appropriate hub class to handle that message, but for this to work hub classes must have a parameterless constructor.
In the real world, a hub class is likely to need dependencies. Fortunately SignalR lets you replace its default "dependency resolver", which I've done. There are numerous (almost identical) samples of SignalR Castle Windsor resolvers (e.g. here), but they all rely on calling the Windsor container's Resolve() method. My understanding of Windsor is that you must "release what you explicitly resolve", but I can't see where I could achieve this.
Note that the hubs are registered as transients, while dependencies may be a mixture of transients and singletons.
I thought about releasing a hub's dependencies in its Dispose() method, but this feels wrong - the hub would need access to the Windsor container; also the hub needs to know which dependencies are transient, and only attempt to release those.
Any thoughts?
IMHO you are bitten by the fact that the signal R dependency resolver fails to implement a Destroy method for releasing the hubs.
I agree that implementing the release of the components in the Hub's dispose method would get rather ugly. To release the hub from windsor the hub would need to call release on the container which in turn would then call the dispose again on the hub.
I think you're best bet would be to create an windsor interceptor for the hub, which will intercept the dispose call on the hub. This way only the interceptor needs to know about the container and you should be able to dispose you component.
You might need some logic in your interceptor to determine whether dispose is called from SignalR or from then windsor container. (ie if called from SignalR call Windsor release, otherwise proceed). You might do this by using e.g Thread local storage.

Any significant use of registering only Service in windsor container?

container.Register(
    Component.For<IMyService>()
);
This code intent to register a service IMyService in Windsor container,but i didn't found any significance use of doing that. am i am missing something here or it is just a syntax practicing?
It depends.
In this case, since your service is an interface it's most likely not terribly useful with default configuration, because you won't be able to instantiate that component most likely.
I'm saying most likely, because you didn't show the definition of the interface, nor the code that configures your container (more on that in a moment).
If your service was a class, it would be a usable registration.
Now the more advanced stuff. In advanced scenarios you can have extensions augmenting your registration, by using custom IContributeComponentModelConstruction, or component model attributes (like InterceptorAttribute) which could extend this simple registration to something more complete.

How would you resolve once and be able to access your logger across your application?

Using castle windsor and the LoggingFacility, how would you resolve once and be able to use your logger across your application?
I understand that bootstrapping/installing is done in global.asax and that is where the resolve should be done but I am confused about how you would access your logger outside of global.asax.
The purpose of using Castle Windsor is to follow the Inversion of Control pattern, whereby you allow the container to inject dependencies (like the ILogger) into classes that require them. You "access the logger" by having your controllers (or whatever classes need to do logging) depend on ILogger, either by taking it as a constructor parameter, or by exposing a settable property of type ILogger.

Castle Windsor - should I reference the container from other classes?

I'm about to embark on a new project using Windsor, but I've been wondering about running into scenarios where a Class A might need to instantiate Class B, but it's not feasible or possible for Windsor to inject an instance of Class B into it. I'm struggling to think of a scenario, but here goes:
Say I have a business entity "Customer" that gets passed to a WCF service. This class has an Ent.Lib self-validation method, which in turn uses a helper class "CustomerValidator". The Customer object received by the service has been deserialized by WCF, so Windsor plays no part in its instantiation, so I can't inject any dependencies. Nor can I pass my CustomerValidator to the self-validation method as it must follow a particular signature for Ent.Lib. So how could I instantiate the CustomerValidator within this class/method? I still want to utilise Windsor rather than simply doing a "var cv = new CustomerValidator();".
It's not a great example as it could be solved in different ways, e.g. passing the Customer object to a validation method rather than having the validation method in the Customer class, but it offers a possible scenario for discussion.
I could expose my WindsorContainer as a public singleton, accessible by any code that needs it, but that seems to be frowned upon. Any other suggestions?
should I reference the container from other classes?
No. By referencing the container, you add complicated and unnecessary dependency to your class which will complicate testing and increase complexity.
The Customer object received by the service has been deserialized by WCF, so Windsor plays no part in its instantiation, so I can't inject any dependencies.
I think this is the direction you should go, try to explore if there really isn't any way to take control of deserialization so you can inject dependencies.
If that fails, consider using http://commonservicelocator.codeplex.com/. Its Microsoft's service location implementation with Windsor adapter available. It's basically the same pattern as if you referenced the container but you don't introduce dependency on specific container implementation. Also I think it will be easier to mock for testing.

Unit testing with a Singleton

I am developing an AS3 application which uses a Singleton class to store Metrics in Arrays. It's a Singleton because I only ever want one instance of this class to be created and it needs to be created from any part of the app.
The difficulty comes when I want to unit test this class. I thought adding public getters and setters would enable me to unit test this properly and would be useful for my app. I have read that changing to a Factory pattern will enable unit testing or using Inversion of control. This would of course make it more flexible too. I would like to know of people's thoughts on this matter as there are SO many conflicting opinions on this!
Thanks
Chris
If you're using an IoC framework, then make your consumers require an instance of the service in their constructor, and configure the IoC framework to only build one instance and keep handing it out to all requests in the entire application. This is the default behavior of Castle Windsor in my experience.
For unit testing you can use a Mock object in place of the real object.