Castle Windsor 3.0, Services and Multiple Implementation
Asked Answered
A

3

5

Having read and googled to the point of exhaustion, I feel as though I may need some guidance.

This may partly be because of the introduction of Castle Windsor 3.0, however most of the blog posts, SO questions and other documentation is explicitly dependant on what I can see is now deprecated code.

So: The problem?

In my application, which is a WCF Service providing back end code to an MVC3 application, I have multiple layers, one of which provides virus scanning services for a file upload system.

The client has asked for support for multiple scan services, naturally I have complied and each scan service implements an IScanService interface, thusly:

public interface IScanService
{
    void Execute();
    ScanResult GetResult();
}

So in the WCf service, where the constructor may look like:

public McAfeeFileScanService(IScanService mcAfeeScanService)
    {
        _scanService = scanService;
    }

How can I specialise that the IScanService which is injected is of implementation type McAfeeScanService, or NortonScanService or other implementation?

AFAIK Windsor by default would provide the first registered implementation, whether that is of type McAfeeScanService or not.

I was looking into ServiceOverrides, however that seems to have been deprecated in Windsor 3.0 in favour of the DependsOn(Dependency.OnComponenent()) [but I have failed to find any relevant examples] Ive also looked into Typed Factory Facilities, and into IHandlerSelector, but feel that some guidance is needed for this (seemingly common and probably simple) task.

Thanks all.

Asperity answered 21/4, 2012 at 20:14 Comment(0)
U
7

ServiceOverride is deprecated in Windsor 3. You have to use Dependency.OnComponent like this:

Component.For<ISerializer>().ImplementedBy<JsonSerializer>().Named("jsonSerializer"),
Component.For<WebConnectionAcceptor, IChannelManager>().ImplementedBy<WebConnectionAcceptor>().Named("webAcceptor"),
Component.For<ConnectionAcceptorProxy>().Named("webProxy").DependsOn(
         Dependency.OnComponent("connectionAcceptor", "webAcceptor"), 
         Dependency.OnComponent("serializer", "jsonSerializer"))

First argument in OnComponent is binding name, second is component name.

Unman answered 23/5, 2012 at 12:39 Comment(1)
Yes, this is the conclusion I came to, cheers for the reply. Ill acept this as answer!Asperity
P
2

Take a look at the second example on the castle Windsor wiki at http://docs.castleproject.org/Default.aspx?Page=Inline-Dependencies&NS=Windsor&AspxAutoDetectCookieSupport=1

It think it is what you are looking for :)

container.Register(     Component.For().ImplementedBy()         .DependsOn(ServiceOverride.ForKey("Logger").Eq("secureLogger"))     );

Regards

Piggin answered 22/4, 2012 at 11:8 Comment(0)
V
0

I posted about something very like this. I had two implementations of an interface and I wanted an MVC Controller to use one of those implementations as decided by me.

In the article I explain one way of doing it, but the attached source code shows two other ways.

Hope this helps.

Vestryman answered 21/9, 2014 at 3:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.