What I want: Resolve object A, and inside object A, I want use same container to resolve object C:
public static void Work()
{
IUnityContainer con = new UnityContainer();
con.RegisterType<IA, A>();
con.RegisterType<IB, B>();
con.RegisterType<IC, C>();
var a = con.Resolve<IA>();
}
interface IA { }
interface IB { }
interface IC { }
class A : IA
{
public A(IB b, IUnityContainer con)
{
for (int i = 0; i < 10; i++)
{
var c = con.Resolve<IC>();
}
}
}
class B : IB { };
class C : IC { };
Problem: On many sites I see that injecting container is bad idea, but how to be in such situation?
EDIT 1
Service Locator is an anti-pattern. So if it is okay to use Service Locator with IoC Container, why is it okay?
ICmultiple times and why do you need to do this insideA's constructor. Can you make your use case more concrete by using real names and describe what you are doing and why? - StevenAandIC. - Steven