[英]DryIOC - overriding the transient/scoped registration for a nested scope
我想覆盖(嵌套)scope 中的瞬态(或作用域)注册。这可能吗? 我发现我可以用 IScope.Use 来实现这一点,但是对于瞬态如何实现呢?
考虑这样一种情况,您可以有选择地从多个自定义项中进行选择,这些自定义项会覆盖某些功能实现。 标准实现在父 scope 中注册,并为所选的自定义创建一个嵌套的 scope,用它自己的实现重新注册受影响的接口。 When different customization is chosen the previous nested scope is disposed and a new one is created with different registrations.
[Test]
public void ScopedTransientDryIOC()
{
var container = new Container(scopeContext: new AsyncExecutionFlowScopeContext());
container.Register<IFeature, StandardFeature>(reuse: Reuse.Transient);
var standardFeature = container.Resolve<IFeature>();
Assert.That(standardFeature, Is.TypeOf<StandardFeature>(), "In container");
using (var scope1 = container.OpenScope("scope1"))
{
// Without overriding resolves the standard implementation from the parent scope
var customizedFeature = container.Resolve<IFeature>();
Assert.That(
customizedFeature,
Is.TypeOf<StandardFeature>(),
"In OpenScope 1st level, before local registration");
// Such a function does not exists. Is there any workaround?
scope1.Register<IFeature, CustomizedFeature>(reuse: Reuse.Transient);
// After overriding it should resolve the customized implementation
customizedFeature = container.Resolve<IFeature>();
Assert.That(
customizedFeature,
Is.TypeOf<CustomizedFeature>(),
"In OpenScope 1st level, after local registration");
}
// When the overriding scope is disposed resolve again the standard implementation from the root scope
var standardFeatureClosed = container.Resolve<IFeature>();
Assert.That(standardFeatureClosed , Is.TypeOf<StandardFeature>(), "After 1st level scope disposed");
}
public interface IFeature {}
public class StandardFeature : IFeature {}
public class CustomizedFeature : IFeature {}
答案是利用子容器:
var container = new Container(scopeContext: new AsyncExecutionFlowScopeContext());
container.Register<IFeature, StandardFeature>(reuse: Reuse.Transient);
var standardFeature = container.Resolve<IFeature>();
Assert.That(standardFeature, Is.TypeOf<StandardFeature>(), "In container");
using (var child = container.CreateChild(IfAlreadyRegistered.Replace))
{
// Without overriding resolves the standard implementation from the parent scope
var customizedFeature = container.Resolve<IFeature>();
Assert.That(
customizedFeature,
Is.TypeOf<StandardFeature>(),
"In OpenScope 1st level, before local registration");
child.Register<IFeature, CustomizedFeature>(reuse: Reuse.Transient);
// After overriding it should resolve the customized implementation
customizedFeature = child.Resolve<IFeature>();
Assert.That(
customizedFeature,
Is.TypeOf<CustomizedFeature>(),
"In OpenScope 1st level, after local registration");
}
// When the overriding scope is disposed resolve again the standard implementation from the root scope
var standardFeatureClosed = container.Resolve<IFeature>();
Assert.That(standardFeatureClosed, Is.TypeOf<StandardFeature>(), "After 1st level scope disposed");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.