Buildmap bootstrap in class / dll library

What is the best approach to loading dll using structure? I don’t want library users to configure anything themselves if they don’t want it. I think .config will most likely be the easiest, but then again will be 2.6.1, and I'm not familiar with many of its features / changes yet.

+3
source share
1 answer

As I mentioned in my comment above, you can use the factory method to ensure that the StructureMap container is deployed and ready to go for the 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>();
}
+5
source

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


All Articles