注册另一个对象的每个生命周期的类

时间:2017-03-12 06:41:28

标签: c# .net dependency-injection inversion-of-control ioc-container

我正在寻找能够执行以下操作的IoC容器:

  1. 假设我有一个可以通过AmbientContext访问的对象,例如LifetimeProvider.Current
  2. 假设我有一个要在应用程序内部某个点解决的类型,例如IDependencyDependency
  3. IDependency的值发生变化时,我希望LifetimeProvider.Current有不同的实例。
  4. IDependency的值为空时,我希望null的值为LifetimeProvider.Current
  5. 这可能吗?

    P.S。以下是案例4)应该通过的测试:

    [Test]
    // Given There Is Lifetime Provider
    // When Dependency Is Registered Per Lifetime And Lifetime Provider Becomes Null
    // Then Dependency Becomes Null
    public void Test()
    {                        
        var container = new SuperDuperContainer();
        container.RegisterPerLifetime(
            () => LifetimeProvider.Current, typeof(IDependency), null, typeof(Dependency));
        var @object = string.Empty;
        LifetimeProvider.Current = @object;
        container.GetInstance(typeof (IDependency), null);            
        LifetimeProvider.Current = null;
        var dependency = container.GetInstance(typeof (IDependency), null);
        Assert.IsNull(dependency);
    }
    

0 个答案:

没有答案