If I’ve got two properties in a class with the same interface type and I want to inject two different conrete types to each how do I do that with autofac either with Property or constructor injection.
eg.
class A : IA
{
public IB PropertyB { get; set; }
public IB PropertyC { get; set; }
public A(IB b, IB c)
{
PropertyB = b;
PropertyC = c;
}
public void PrintB()
{
PropertyB.Print();
}
public void PrintC()
{
PropertyC.Print();
}
}
I’ve tried this but of course I just get a C inject into both properties
var builder = new ContainerBuilder();
builder.RegisterType<B>().As<IB>();
builder.RegisterType<C>().As<IB>();
builder.RegisterType<A>().As<IA>();
var container = builder.Build();
var a = container.Resolve<IA>();
Or this with the same result:
builder.RegisterType<B>().As<IB>();
builder.RegisterType<C>().As<IB>();
builder.RegisterType<A>().As<IA>().PropertiesAutowired();
var container = builder.Build();
var a = container.Resolve<IA>();
Is there a way I can tell autofac that I want B in PropertyB and C in PropertyC ?
Using property injection, you can do the following: