awilinsk
awilinsk

Reputation: 2044

StructureMap configure a profiled nested container

I've come across an unexpected outcome when trying to resolve an instance. It's probably better to explain in code. Here is the Registry used for 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>();
    }
}

In a factory class I build up a nested container and register the current screen with the container like so:

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

In one of my commands it has a dependency for an IScreen, but instead of receiving the "screen" instance I configured for the nested container, it receives a NullScreen.

Is there something I'm doing wrong or will profiled nested containers not support this scenerio?

Upvotes: 1

Views: 618

Answers (1)

awilinsk
awilinsk

Reputation: 2044

I'm going with the solution of calling nestedContainer.EjectAllInstancesOf(). I still do not know why specifying the profile for a container would cause it to change the default instance of IScreen.

Upvotes: 1

Related Questions