tags:

views:

161

answers:

1

What is the best approach to bootstrap a dll using structuremap? I don't really want the consumers of the library to have to configure anything themselves if they don't want to. I am thinking that the .config would likely be the simplest, but then again 2.6.1 is out and I am not familiar with many of its features / changes yet.

+1  A: 

As I mentioned in my comment above, you could use a factory method to ensure that the StructureMap container is spun up and ready to go for top level classes in your library. Here is an example.

public interface ILibraryClass
{
    void SomethingAwesome();
}

public class LibraryClass : ILibraryClass
{
    public void SomethingAwesome()
    {
    }
}

public class API
{
    private static IContainer _container;

    private static IContainer Container
    {
        get
        {
          if (_container == null) //TODO add locking around this for thread safety?
             InitializeContainer();

          return _container;
        }
    }

    private static void InitializeContainer()
    {
        _container = new Container(config => { config.For<ILibraryClass>().Use<LibraryClass>(); });
    }

    public static ILibraryClass LibraryClass()
    {
        return Container.GetInstance<ILibraryClass>();
    }
 }

[Test]
public void library_factory_method()
{
    API.LibraryClass().ShouldBeOfType<LibraryClass>();
}
KevM