c a n d l a n d . n e t

Castle microkernel Shows It’s strength

Dusty Candland | |

I'm a big fan of the CastleProject.org stack, but the MicroKernel really shows it's strength when you need to do a couple of custom things, and find out that the customization is really easy, and intuitive.

Setting up a facility to add a custom bunch of objects. I think there are already facilities to do this, but it’s really simple for standard cases and allows you to easily get things setup. You basically just need to override the Init method.

protected override void Init()
{

List<Assembly> assemblies = ReadConfigForAssemblies(FacilityConfig);
<span class="kwrd">foreach</span> (Assembly assembly <span class="kwrd">in</span> assemblies)
    LoadCommands(assembly);

}

The configuration is already read in for you, so parsing that is easy.

public List<Assembly> ReadConfigForAssemblies(IConfiguration facilityConfig)
{
    List<Assembly> assemblies = new List<Assembly>();
    try
    {
        foreach (IConfiguration configuration in facilityConfig.Children)
            if (configuration.Name.Equals("assembly", StringComparison.InvariantCultureIgnoreCase))
                assemblies.Add(Assembly.Load(configuration.Value));
    }
    catch (Exception e)
    {
        throw new ConfigurationErrorsException(
            "Assembly could not be loaded, check you have the correct name in the configuration.", e);
    }
    return assemblies;
}

Lastly, you can search through the configured assemblies and register any types that match what your looking for.

private void LoadCommands(Assembly assembly)
{
    foreach (Type type in assembly.GetTypes())
        if (typeof (IMyInterface).IsAssignableFrom(type) && !type.IsAbstract && type.IsPublic)
            Kernel.AddComponent(type.FullName, type);
}

This is getting to be a long post, but I'm sure you're wondering about testing. It's easy too! You have to mock out the configuration stuff, but that's really it. I just put into a setup method and once done you have your container setup and ready for testing.

[SetUp]
public void Setup()
{
    _container = new WindsorContainer();
    _facility = new CommandFacility();

    _mocks = new MockRepository();
    IConfiguration config = mocks.DynamicMock<IConfiguration>();
    IConfiguration configChild1 = mocks.DynamicMock<IConfiguration>();
    IConfiguration configChild2 = mocks.DynamicMock<IConfiguration>();

    using (_mocks.Record())
    {
        Expect.Call(config.Children).Return(new ConfigurationCollection(new IConfiguration[] { configChild1 }));
        Expect.Call(configChild1.Name).Return("assembly");
        Expect.Call(configChild1.Value).Return("MyAssemblyName");
    }

    using (_mocks.Playback())
    {
        _facility.Init(_container.Kernel, config);
    }
}

There's more documentation on the Castle site. Next time custom dependency resolvers and sub kernels...

Webmentions

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