所以,我试图解决一个我确定别人已经遇到的问题。基本上,我希望对IoC容器的调用递归解决依赖关系,但也可能执行一些自定义代码,以根据一组预定义条件更改结果。这是模糊的,所以让我举一个例子:Unity IOC递归 - 拦截器的使用?
说我有一个控制器,像这样:
public class SampleController : Controller
{
protected SampleType _sampleType = null;
public SampleController(SampleType sampleType)
{
_sampleType = sampleType;
}
}
我也得到了该控制器的一些测试版本(比如我重构它,我想使确保它不被AB可怕打破督促测试它的曝光):
public class SampleController_V2 : SampleController
{
protected SampleType _sampleType = null;
protected AnotherType _anotherType = null;
public SampleController_V2(SampleType sampleType, AnotherType anotherType)
{
_sampleType = sampleType;
_anotherType = anotherType;
}
}
我已经延长了DefaultControllerFactory
创建控制器时使用统一。这一切工作正常。现在,我想要做什么,它提供了如果需要解决的问题,可以在层次结构中测试任何特定类型的功能。这适用于顶层,但不适用于通过对象图递归的子元素。
现在,它会选择适当的控制器来解决并赋予其依赖关系。然而,我似乎无法拦截个别调用依赖关系,也AB测试这些。我可以通过数据库配置定义一个测试,然后让IOC容器根据标准解决它。例如:
SessionIds that start with the letter 'a': SampleController_V2
Everyone Else : SampleController
UserIds ending in 9 : SampleType_V2
Everyone Else : SampleType
这一切都适用于顶级项目。但是,拨打_unityContainer.Resolve(type)
似乎不是一个递归调用;我希望能够注入代码到任意点时,它试图解决型:
-> Attempt to Resolve SampleController
-> Test Code Tells Us to use _V2 if possible.
-> Attempt to Resolve SampleType
-> Test Code tells us to use the _V1 if possible.
-> Resolves SampleType_V1
-> Attempt to Resolve AnotherType
-> No Test Defined, Use the Default
-> Resolves AnotherType
-> Resolves SampleController_V2 (passing SampleType_V1 as the dependency and then AnotherType as the other dependency)
通过一些网上的文章看,这听起来像我需要使用某种形式的统一拦截,但它的几乎就像我正在编写我自己的IoC容器,并且内置了某种测试体系结构。
希望有人对如何在找到构造函数并解决问题之前要做到这一点有个好主意每种类型递归。
编辑:所以它实际上并没有证明这是可怕的创建我自己的注入通过递归地检查每个依赖项的构造函数参数,但我认为如果我为我自己抛出Unity,老板们可能会有点不安。定制解决方案
我可能会误解。您想要根据输入的测试数据解析不同的对象图吗?我不确定你为什么要这样做 - 当我用容器测试时,我尽量保持对象图尽可能接近生产。 –
这是为特定对象提供AB测试。例如:假设我有一个EmailSender类。它确实是X.但是,我想测试一个重写的EmailSender类。我有国际奥委会容器解决一个EmailSender_V2类在一组情况下会做Y.说它可能是性能密集型的;我想慢慢介绍一下它是否会对网站产生负面影响。然后,我可以上升到100%,并将代码转移到基本版本,而无需修改控制版本。大多数用户获得相同的体验;一些用户在测试时获得增强的体验。 – Tejs