Chuck Conway

In The craft

StructureMap: Configure or Initialize?

May 26, 2012 · 1 minute read

StructureMap is a rock’in IOC. The general idea of registering groups of dependencies based conditions makes it a pleasure to use and I have tried them all (well except Unity). The downside to StructureMap is it’s documentation. It suu-cks! The StructureMap site looks like it was designed in the 80’s. When searching Google for help many of the hits are 3 to 4 years old. Weak.

Here is my bit of love to Structure map. It took me forever to figure this out. There is even a post on it by Jeremy Miller (Creator of StructureMap). But, ya you guessed, it’s 4 years old.

I don’t understand the difference between Initialize and Configure. At first I thought one was eager loading versus lazy loading, but that did not make sense. All I know is I move my Setup into the Initialize method and things magically started working.

ObjectFactory.Initialize(
    a =>; 
    {
        a.Register(new SubpoenaKeysValidator());
        a.For().Use(new MsSqlDatabase());
        a.For().Use();
        a.RegisterInterceptor(new LogInterceptor());

        a.Scan(s=>
                   {

                       s.TheCallingAssembly();
                       s.AssemblyContainingType();
                       s.WithDefaultConventions();

                       s.ConnectImplementationsToTypesClosing(typeof (AbstractValidator));
                       s.ConnectImplementationsToTypesClosing(typeof(IFormHandler));
                       s.ConnectImplementationsToTypesClosing(typeof(IQueryHandler));
                   }
            );
    });

ObjectFactory.AssertConfigurationIsValid();