StructureMap配置已分析的嵌套容器

时间:2011-01-17 17:53:15

标签: structuremap

尝试解析实例时,我遇到了意想不到的结果。在代码中解释可能更好。这是用于ObjectFactory.Initialize的注册表:

public MyRegistry : Registry {
    public MyRegistry() {
        this.For<IServiceA>.Use<ServiceA>();
        this.For<IServiceA>.Use<SpecialServiceA>().Named("Special");
        this.Profile("Special", p => p.For<IServiceA>().Use("Special"));

        this.For<IScreen>().Use<NullScreen>();
    }
}

在工厂类中,我构建了一个嵌套容器,并使用容器注册当前屏幕,如下所示:

public void ProcessScreenRequest(IScreen screen) {
    using(IContainer nestedContainer = this._container.GetNestedContainer(screen.Profile)) {
        nestedContainer.Configure(x => x.For<IScreen>().Use(screen);
        //process chain of commands to display screen.
    }
}

在我的一个命令中,它具有IScreen的依赖关系,但是它不是接收我为嵌套容器配置的“屏幕”实例,而是接收NullScreen。

有什么我做错了,或者有资料的嵌套容器不支持这个场景吗?

1 个答案:

答案 0 :(得分:1)

我将使用调用nestedContainer.EjectAllInstancesOf()的解决方案。我仍然不知道为什么指定容器的配置文件会导致它更改IScreen的默认实例。

相关问题