I'm trying to get to grips with IoC containers (Unity specifically at the moment), and am having a bit of struggle with the concept of injecting all dependencies up front.
My problem is specifically related to a class that has a constructor parameter that has a value that isn't known when I initially register the types in the container.
Here's a little snippet that should illustrate what I'm talking about.
class Class1
{
IUnityContainer uContainer;
public Class1()
{
uContainer = new UnityContainer();
uContainer.RegisterType<IRepo, Repo>(new ContainerControlledLifetimeManager()));
Class2 cls2 = uContainer.Resolve<Class2>();
cls2.DoSomething();
}
}
class Class2
{
IRepo _repo;
public Class2(IRepo p_repo)
{
_repo = p_repo;
}
public void DoSomething()
{
IType2 typ2 = new Type2(_repo.SomeDataRetrieved());
Class3 cls3 = new Class3(_repo, typ2);
}
}
class Class3
{
IRepo _repo;
IType2 _type2;
public Class3(IRepo p_repo, IType2 p_type2)
{
_repo = p_repo;
_type2 = p_type2;
}
}
I can set up the container in Class1 and have the Repo injected into Class2 using the UnityContainer. In Class2, some lookup against the Repo is done returning an instance of Type2 that is only possible to instantiate in Class2. I then need to pass the Repo to Class3 together with the new object created of Type2.
The problem is twofold:
- I can't resolve Class3 in Class1 because it can only be instantiated in Class2.
- I can't register Type2 in the container because it actually has to have a value (not a default one) based on the output of a call in Class2, which is in a different scope from where the Container existed and did the registrations.
As it stands, I can inject the dependancies using the container into Class2, but for Class3 I need to create new instances or using contructor injection from 2 to 3, which then makes me wonder why I'm using the Container then if I have to resort to manual injection anyway.
So, how do I register Class3 in the container so that when I instantiate it, I Inject the singleton of the repo that was registered in the container at the beginning as well as the instance of Type2 that was created in DoSomething.
Thanks in advance for the help.