Suppose I have the following hierarchy which I wish to instantiate with DI:
DatabaseService(IDatabaseRepository) : IDatabaseService
Database1Service(IDatabaseRepository) : IDatabase1Service
Database2Service(IDatabaseRepository) : IDatabase2Service
DatabaseRepository(IUnitOfWork) : IDatabaseRepository
DatabaseRepository2(IUnitOfWork) : IDatabaseRepository
UnitOfWork(string) : IUnitOfWork
Is there a method in Stucturemap that would let me do this?
public class MyRegistry : Registry
{
public MyRegistry()
{
//default-convention-scan code here
//this is the unit of work for almost all of my interface implementations
//i.e. new DatabaseService(new DatabaseRepository(new IUnitOfWork("Database1")))
// new Database1Service(new DatabaseRepository(new IUnitOfWork("Database1")))
For<IUnitOfWork>()
.Use(new UnitOfWork("Database1"));
//Database2Service needs a separate implementation Of UnitOfWork
//i.e. new Database2Service(new DatabaseRepository(new IUnitOfWork("Database2")))
For<IUnitOfWork>().IfDescendantDependencyOf<IDatabase2Service>()
.Use(new UnitOfWork("Database2"))
}
}
If it's not clear, I want all injected IUnitOfWork
s to point to the singleton new UnitOfWork("Database1")
, except if it was injected while trying to construct an instance of IDatabase2Service
, where it should use the singleton new UnitOfWork("Database2")
.