-1

For historic reasons, our application has a single static StructureMap Container which is used for all IoC/DI everywhere. We have quite a few tests which check that this container is properly set up, but it is becoming increasingly difficult to ensure that tests don't interfere with each other. I would like to start breaking out to several (non-static) containers to alleviate this. To retain backward compatibility, things configured in this container must be available from the main container still.

I have implemented a proof-of-concept for a small part of the system which handles only a few base types this way:

_pocContainer = new Container(/* setup code */);
MainContainer.Configure(c => c.For<C>.Use(ctx => _pocContainer.GetInstance<C>());

This works, and when requesting an instance of C from the static container, it is resolved from the _pocContainer. However, I'm fairly certain that this isn't something that was considered a normal use case when designing StructureMap. Will this cause bad performance, or have any other negative consequences? Is there some better way to do this?

2
  • Can't you divide the registration into Registries and test each Registry separately?
    – PHeiberg
    Aug 22, 2013 at 19:09
  • @PHeiberg: Good idea! Implemented it this way - please post as answer :)
    – carlpett
    Aug 26, 2013 at 7:53

1 Answer 1

1

One possible approach is to divide the registration into Registries and test each registry separately.

public class FooRegistry : Registry {
    public FooRegistry() {
        For<IFoo>().Use<Foo>();
    }
}

You can then explicitly use the registry in the test

var container = new Container(new FooRegistry());

But have all registries automatically scanned in your composition root

var container = new Container(x => {
    x.Scan(scanner =>
    {
        scanner.TheCallingAssembly()
       scanner.LookForRegistries();
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.