We have 3 diffrent classes that are linked. We register this in unity.
We have problems resolving them correctly.
The classes look like this:
public class A: IA
{
public IB B { get; private set; }
public IC C { get; private set; }
public A(IB b, IC c)
{
this.B = b;
this.C = c;
}
}
public class B : IB
{
public IC C { get; private set; }
public B(IC c)
{
this.C = c;
}
}
public class C :IC{}
public interface IA
{
IB B { get; }
IC C { get; }
}
public interface IB{
IC C { get; }
}
public interface IC{}
The unity config looks like this:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
<register type="IA" mapTo="A" />
<register type="IB" mapTo="B" />
<register type="IC" mapTo="C" />
</container>
</unity>
This is our Main class where we are trying to Resolve A from unity and get the same C object in both the A object and the B object.
public class Main
{
public Main()
{
//This works..
IC objC = new C();
IB objB =new B(objC);
IA objA = new A(objB,objC);
//We want to do this.
//How do we register in Unity to ensure we get the same object for C in both A and B
IA obj1 = Unity.Resolve<IA>();
Debug.Assert(obj1.C == obj1.B.C);
//It's very important that two diffrent resolved objects of A don't have the same C object
IA obj2 = Unity.Resolve<IA>();
Debug.Assert(obj2.C == obj2.B.C);
Debug.Assert(obj1.C != obj2.C);
}
}
Is this possible to achive in unity? Maybe by using some Lifetime Manages?