1
votes

I am using Castle Windsor and here is what i want to do.

ParentContainer has CarFactory depending on abstract WheelFactory but WheelFactory would be registered in child containers 1 and 2

ChildContainer 1 has WheelFactory as BigWheelFactory ChildContainer 2 has WheelFactory as SmallWheelFactory

Now we have [ParentContainer (ChildContainer 1, ChildContainer 2)]

Thread 1 uses only ChildContainer 1

Thread 2 uses only ChildContainer 2

Thread 1 ask for CarFactory from ChildContainer 1, car factory should use BigWheelFactory in this case.

Thread 2 ask for CarFactory from ChildContainer 2, car factory should use SmallWheelFactory in this case.

how can i achieve this with Castle Windsor. Even if means not using child containers

1
If by "process", you are referring to executable processes, each should have their own DI configuration that is completely separate from the other processes. While it is possible to serialize objects to send them from one process to another, that is a runtime design feature of an application, not something that pertains to DI, which is only for composing applications. If that is not what you mean by process, I suggest you change your question to clarify your actual meaning.NightOwl888
@NightOwl888 suggestion acknowledged. ThanksGurpreet

1 Answers

1
votes

Instead of child container try standard service override:

var container = new WindsorContainer();

container.Register(Component.For<HostForThread1>().DependsOn(Property.ForKey<CarFactory>().Is("CarFactory1")));
container.Register(Component.For<HostForThread2>().DependsOn(Property.ForKey<CarFactory>().Is("CarFactory2")));

container.Register(Component.For<CarFactory>().DependsOn(Property.ForKey<WheelFactory>().Is<BigWheelFactory>()).Named("CarFactory1"));
container.Register(Component.For<CarFactory>().DependsOn(Property.ForKey<WheelFactory>().Is<SmallWheelFactory>()).Named("CarFactory2"));

container.Register(Component.For<WheelFactory>().ImplementedBy<BigWheelFactory>());
container.Register(Component.For<WheelFactory>().ImplementedBy<SmallWheelFactory>());


public class HostForThread1
{
    public HostForThread1(CarFactory factory)
    {
        this.Factory = factory;
    }

    public CarFactory Factory { get; }
}

public class HostForThread2
{
    public HostForThread2(CarFactory factory)
    {
        this.Factory = factory;
    }

    public CarFactory Factory { get; }
}

public class CarFactory
{
    public CarFactory(WheelFactory wheelFactory)
    {
        this.WheelFactory = wheelFactory;
    }

    public WheelFactory WheelFactory { get; }
}

public abstract class WheelFactory
{
}

public class BigWheelFactory : WheelFactory
{
}

public class SmallWheelFactory : WheelFactory
{
}