Profile picture Schedule a Meeting
c a n d l a n d . n e t

Making Sure Your Container Contains Your Basic Services

Dusty Candland | |

I'm working on creating some of the base code for our systems. One of the things we're doing is moving to using IoC containers for our projects. These projects are all internal and as such they will be sharing some base functionality, like logging and configuration. To that end I wanted to create an implementation of WindsorContainer which has these services and facilities already setup. What I found was the IWindsorInstaller class. Using that in the constructor of my NewWindsorContainer I can call Install with any number of these installers, like:

public class MyContainer : WindsorContainer
{

<span class="kwrd">public</span> MyContainer(<span class="kwrd">string</span> settingName)
{
    Install(<span class="kwrd">new</span> InstrumentationInstaller());
}

}

The Installer interface is just one method Install which takes a container and a configuration store. This makes these installer classes easy to implement and test. Below we just make sure the logging facility is registered.

public class InstrumentationInstaller :IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<MyLoggingFacility>();
    }
}

And the test code:     

[Test]
public void Install_Expect_Facility_Installed_Can_Resolve_Logger()
{
    var installer = new InstrumentationInstaller();
    var c = new WindsorContainer();
    installer.Install(c, null);
    var logger = c.Resolve<IMyLogger>();
    Assert.IsNotNull(logger);
}

I was initially going to create a facility to load our base services, but I like this approach more because it's simpler and the installer classes follow Single Responsibility Principle.

Webmentions

These are webmentions via the IndieWeb and webmention.io. Mention this post from your site: