Registration of a common interface and implementation in a nested container in the form of a table

After switching from StructureMap 2.6.4 to StructureMap 3.1.6, registration for our common interfaces, which we have in our nested containers, stopped working, and I can not find the reason why they no longer work.

This is the error I get

StructureMap.StructureMapConfigurationException: The default instance is not registered and cannot be automatically determined for type "ITest <ExtendClass>"

Here is an example demonstrating the problem:

public class StructureMapContainerTests { [Test] public void GlobalContainer() { var container = new Container(); container.Configure(x => x.For(typeof(ITest<>)).Use(typeof(Test<>))); var impl = container.GetInstance<ITest<ExtendClass>>(); Debug.WriteLine(impl.Temp()); //Works } [Test] public void NestedContainer() { var container = new Container(); var nestedContainer = container.GetNestedContainer(); nestedContainer.Configure(x => x.For(typeof(ITest<>)).Use(typeof(Test<>))); var impl = nestedContainer.GetInstance<ITest<ExtendClass>>(); Debug.WriteLine(impl.Temp()); //Doesn't work } } public interface ITest<T> where T : BaseClass { string Temp(); } public class Test<T> : ITest<T> where T : ExtendClass { public string Temp() { return "OK: " + typeof(T); } } public class BaseClass { } public class ExtendClass : BaseClass { } 

Both of the above tests work in StructureMap 2.6.4, but in 3.1.6 the second test, which uses a nested container, no longer works.

Does anyone know if this function has been removed or am I just using it incorrectly and in this case can help me? :)

+5
source share
1 answer

In my experience, you can register neither generic types nor decorators inside nested StructureMap containers.

The way we worked was to split the registry into 2 registries, one of which contains generics and decorators that are registered in the parent container, and the other with non-primary interfaces registered in the nested container. Unfortunately, this is not always easy to do when creating an extensible system.

Here's Jeremy's answer to Tobias from the Google Group for someone else interested.

The nested container architecture has been completely rewritten for version 3.0. Honestly, I think the fact that it worked at 2.6. *, Just a fluke.

This type of registration on a nested container is currently not supported ...

Jeremy D. Miller

0
source

Source: https://habr.com/ru/post/1247048/


All Articles