使用AutoFac获取IRepository的已解决依赖关系 [英] Get resolved dependancies of IRepository with AutoFac

查看:171
本文介绍了使用AutoFac获取IRepository的已解决依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有如下设置的接口:

public interface IRepository { };

public interface IFirstRepository : IRepository { };
public interface ISecondRepository : IRepository { };
public interface IThirdRepository : IRepository { };

和需要一些但不是全部这些存储库的Controller:

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;
    }
}

BaseController 看起来像:

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?
     }
}

有没有办法 BaseController 可以利用AutoFac上下文和现有信息来获取 TestController IRepository 实例的集合。 c>?

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肯定已经有了该信息,因为它能够使用正确的实例调用构造函数。

Autofac surely already has that info since it was able to call the constructor with the correct instances.

NB:我在这里不仅仅使用反射的唯一原因就是性能问题。

NB: The only reason I don't just use reflection here is performance concerns.

推荐答案

没有一种简单的方法可以解决此问题盒子。如果您发现自己处于这种情况,则通常意味着您遇到了界面设计问题。 也有一个常见问题解答,介绍了为什么这样做作为某些选项。

There's not an easy way to do this out of the box. If you find yourself in this situation, generally it means you have an interface design problem. There's an FAQ walking through an example of why this is, as well as some options.

如果确实需要这样做,那么我可能会考虑添加每个注册的元数据,其中每个控制器都有一个元数据条目,需要该特定存储库,然后使用元数据过滤器属性。再次,我提到的常见问题解答将逐步解决显示示例。

If you really, really need to do this, then I'd probably look at adding metadata to each of the registrations where you have a metadata entry for each controller requiring that particular repository, then filter at the constructor using the metadata filter attribute. Again, the FAQ I mentioned walks through this and shows examples.

但是如果 me 在此系统中工作……我会退后一步,看看我的界面设计以了解如何逃脱从一开始就不需要这种过滤器。如果不能均等地对待它们(Liskov替换原理),则表明需要使用不同的接口。

But if it was me working in this system... I'd step back and look at my interface design to see how to get away from needing this sort of filter in the first place. If they all can't be treated equally (Liskov substitution principle) then it indicates there are different interfaces required.

这篇关于使用AutoFac获取IRepository的已解决依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆