I am new to IoC and Castle Windsor.
The question is more related to IoC, but i just chose Castle as my weapon of choice.
I immediately ran into a problem that it seems impossible to construct the container from a config file or from a singleton.
The only way seeming to work for me is to use a Builder function that will construct the whole container each time and then let me specify the types, look at this example:
I have a view:
public interface IView
{
}
There are 2 concrete implementations of this view:
public class ConcreteViewA : IView
{
}
public class ConcreteViewB : IView
{
}
I have a controller that operates on the view:
public class Controller
{
public Controller(IView view) { }
public void Load() { }
}
I create a function that constructs my IoC container and register the common types, the only type i can register is my controller, as this is all that stays the same:
WindsorContainer BuildContainer()
{
var container = new WindsorContainer();
container.Register(Component.For<Controller>().ImplementedBy<Controller>());
return container;
}
I have 2 (Root) entry points:
void RootMethod1()
{
var container = BuildContainer();
container.Register(Component.For<IView>().ImplementedBy<ConcreteViewA>());
var controller = container.Resolve<Controller>();
controller.Load();
}
void RootMethod2()
{
var container = BuildContainer();
container.Register(Component.For<IView>().ImplementedBy<ConcreteViewB>());
var controller = container.Resolve<Controller>();
controller.Load();
}
As you can see I have to rebuild the container each time as i need to tell it what to use for IView for each of the context i am in. What if the Container is expensive to rebuild (Lots of types etc), how can I design this?