With interfaces set up like so:
public interface IRepository { };
public interface IFirstRepository : IRepository { };
public interface ISecondRepository : IRepository { };
public interface IThirdRepository : IRepository { };
And a Controller that needs some but not all of these repositories:
public class TestController : BaseController
{
private IFirstRepository mFirstRepository;
private ISecondRepository mSecondRepository;
// Standard DI for MVC
public TestController(IFirstRepository first, ISecondRepository second)
{
mFirstRepository = first;
mSecondRepository = second;
}
}
And BaseController
looks like:
public class BaseController : Controller {
public IEnumerable<IRepository> GetRepos()
{
// One solution is to use reflection
// to get all properties that are IRepositories.
// Something along the lines of;
return typeof(this).GetProperties().Where(prop=>prop is IRepository);
// But is there a way to use the AutoFac context
// to get the repos, rather than use reflection?
// it surely already has that info since it
// was able to call the constructor correctly?
}
}
Is there a way BaseController
can utilize the AutoFac context and existing info to get the collection of the IRepository
instances requested by the TestController
?
Autofac surely already has that info since it was able to call the constructor with the correct instances.
NB: The only reason I don't just use reflection here is performance concerns.
Aucun commentaire:
Enregistrer un commentaire