I have 2 classes that implement IMyInterface
, lets call them ClassA
and ClassB
. The problem here is that ClassA
also needs IMyInterface
, which should be the implementation of ClassB
. And there's ClassConsumer
which should use ClassA
(the one who is IMyInterface
and depends on IMyInterface
)
Is there a way to do something like?:
var container = new UnityContainer();
//... several others registrations
container.RegisterType<ITransactionProcessingService, ClassConsumer>();
var specialRegistrationForIMyInterface = _container.RegisterType<IMyInterface, ClassB>();
container.RegisterType<IMyInterface, ClassA>(specialRegistrationForIMyInterface);
Code Samples:
public interface IMyInterface
{
void PushToDatabase();
}
public class ClassA : IMyInterface
{
IMyInterface _IMyInterface;
public ClassA(IMyInterface myInterface)
{
_IMyInterface = myInterface;
}
public void PushToDatabase()
{
//do other stuff
//BusinessCheck();
//Log();
//Cache();
_IMyInterface.PushToDatabase();
}
}
public class ClassB : IMyInterface
{
public void PushToDatabase()
{
//Actual insert on database
}
}
Edit 2: Code after Progman suggestion:
var container = new UnityContainer();
//... several others registrations
container.RegisterType<IMyInterface, ClassB>("Foo");
container.RegisterType<IMyInterface, ClassA>("Bar", new InjectionConstructor(new ResolvedParameter<IMyInterface>("Foo")));
container.RegisterType<ITransactionProcessingService, ClassConsumer>(new InjectionConstructor(new ResolvedParameter<IMyInterface>("Bar")));
and I get an error "No member matching data has been found."