2
votes

Situation: I have an interface, such as

public interface ITestClass
{
    string StringElement { get; }
}

There is an implementation for it:

public class TestClassImplementor :
    ITestClass
{
    public string StringElement
    {
        get { return "AAA"; }
    }
}

I do have a factory that creates an implementation instance:

    public class TestClassFactory
    {
    public ITestClass Create()
    {
    return new TestClassImplementor();
    }
}

I want to be able to resolve the implementation using Castle Windsor configuration file (XML), but at the same time not configuring the implementation for the interface.

Why is this needed: resolving (web) services. I do have a (web) service interface, but I don't access the implementation, as it is on another component. I want to be able to simply type MyCastleUtility.Resolve<ITestClass>() and get a prepared web service. I want to use Castle Windsor configuration file (XML) to configure the service I'm trying to access.

The problem: if I can't access the implementation, I can't configure it in the configuration file.

What I've tried so far: 1) Factories.

        <component id="mycompfactory"
  type="MyTestProject.TestClassFactory, MyTestProject"/>

        <component id="mycomp"
          type="MyTestProject.ITestClass, MyTestProject"
          factoryId="mycompfactory" factoryCreate="Create" />

I do get the: Castle.MicroKernel.ComponentRegistrationException : Type MyTestProject.INewTestClass is abstract. As such, it is not possible to instansiate it as implementation of MyTestProject.INewTestClass service

2) Proxies. Hit the wall when tried to find a way to configure that "proxy must be created for 'ITestClass' interface".

Target: configure Castle Windsor to create interface implementation without directly accessing the class, that implements the interface.

Help is much appreciated, Mindaugas

1
What do you mean by "I can't access the implementation". Why can't you access it? Do you have to use XML?Krzysztof Kozmic
> Why can't you access it? Let's say we have two services in the system: Service1 and Service2. Service1 accesses Service2 via web service interface. Service1 uses Service2's IService2 interface, because Service1 references Service2.Interfaces assembly, but it doesn't directly reference Service2.Application assembly, where the implementation for IService2 interface resides. That's by design and I think that's correct. > Do you have to use XML? In general, yes. Components configuration is done in the XML and really wouldn't like to bring an exception in this case, i.e. use code instead of XML.Mindaugas

1 Answers

1
votes

For the factory approach:

  1. Did you also configure the FactoryFacility in your config?

  2. In your "mycomp", I think you want to use "service" and not "type":

<component id="mycomp" service="MyTestProject.ITestClass, MyTestProject" factoryId="mycompfactory" factoryCreate="Create" />