Configuring Castle Windsor

Note

Miruken has first class integration with Castle Windsor, but Miruken does not require you to use Castle Windsor for your container. Miruken does not even require you to use a container. All of that being said, we love Castle Windsor and use it in our own projects.

One of the main ways of configuring a Castle Windsor container is the Container.Install() method. It accepts a comma seperated list of IWindsorInstaller instances. These installers do all the work of registering objects and configuring the container.

In this very basic Castle Windsor Container all the IWindsorInstaller classes in the current assembly will be run. FromAssembly.This() returns an IWindsorInstaller.

namespace Example.MirukenCastleExamples
{
    using Castle.MicroKernel.Resolvers.SpecializedResolvers;
    using Castle.Windsor;
    using Castle.Windsor.Installer;

    public class BasicWindsorContainer
    {
        public IWindsorContainer Container { get; set; }

        public BasicWindsorContainer()
        {
            Container = new WindsorContainer();
            Container.Kernel.Resolver.AddSubResolver(
                new CollectionResolver(Container.Kernel, true));
            Container.Install(FromAssembly.This());
        }
    }
}

We used to use this simple form of configuration, but found that we had to list assemblies multiple times. Features and FeatureInstaller solve this problem.

Features

At a high level a feature is an implementation of a Miruken concept. It may be a Protocol, Handler, Validator, or Mediator, etc. On a very practical level features are concrete application code implemented across multiple assemblies. The Features object has several ways to specify your application assemblies so that they can be installed in the container. Using Features allows you to specify all your assemblies in one place.

FeatureInstaller

FeatureInstallers inherit from FeatureInstaller and do the container registration and configuration for a Miruken concept across all your feature assemblies.

FromAssemblies(params Assembly[] assemblies)

In this example we pass a comma seperated list of application assemblies into:

Features.FromAssemblies()
typeof(CreateTeam).Assembly Targets the Example.League assembly.
typeof(CreateStudent).Assembly Targets the Example.School assembly.

Next, we specify which FeatureInstallers the application needs. This example configures the ConfigurationFactory using the ConfigurationFactoryInstaller, and Validation using the ValidationInstaller.

namespace Example.MirukenCastleExamples
{
    using Castle.MicroKernel.Registration;
    using Castle.MicroKernel.Resolvers.SpecializedResolvers;
    using Castle.Windsor;
    using League;
    using Miruken.Castle;
    using Miruken.Validate.Castle;
    using School;

    public class FeaturesFromAssemblies
    {
        public IWindsorContainer Container { get; set; }

        public FeaturesFromAssemblies()
        {
            Container = new WindsorContainer();
            Container.Kernel.Resolver.AddSubResolver(
                new CollectionResolver(Container.Kernel, true));

            Container.Install(
                new FeaturesInstaller(
                    new ConfigurationFeature(), new ValidateFeature())
                    .Use(Classes.FromAssemblyContaining<CreateTeam>(),
                         Classes.FromAssemblyContaining<CreateStudent>())
            );
        }
    }
}

FromAssembliesNamed(params string[] assemblyNames)

The FromAssembliesNamed() method allows you to specify the assembly name of the feature assemblies you want installed into the container.

namespace Example.MirukenCastleExamples
{
    using Castle.MicroKernel.Registration;
    using Castle.MicroKernel.Resolvers.SpecializedResolvers;
    using Castle.Windsor;
    using Miruken.Castle;
    using Miruken.Validate.Castle;

    public class FeaturesFromAssembliesNamed
    {
        public IWindsorContainer Container { get; set; }

        public FeaturesFromAssembliesNamed()
        {
            Container = new WindsorContainer();
            Container.Kernel.Resolver.AddSubResolver(
                new CollectionResolver(Container.Kernel, true));

            Container.Install(
                new FeaturesInstaller(
                    new ConfigurationFeature(), new ValidateFeature())
                        .Use(Classes.FromAssemblyNamed("Example.League"),
                             Classes.FromAssemblyNamed("Example.School"))
            );
        }
    }
}

InDirectory(AssemblyFilter filter)

The InDirectory() method allows you to specify an AssemblyFilter. An AssemblyFilter takes the string name of a directory and a filter predicate to allow only the assemblies you intend.

namespace Example.MirukenCastleExamples
{
    using Castle.MicroKernel.Registration;
    using Castle.MicroKernel.Resolvers.SpecializedResolvers;
    using Castle.Windsor;
    using Miruken.Castle;
    using Miruken.Validate.Castle;

    public class FeaturesInDirectory
    {
        public IWindsorContainer Container { get; set; }

        public FeaturesInDirectory()
        {
            Container = new WindsorContainer();
            Container.Kernel.Resolver.AddSubResolver(
                new CollectionResolver(Container.Kernel, true));

            Container.Install(
                new FeaturesInstaller(
                    new ConfigurationFeature(), new ValidateFeature())
                    .Use(Classes.FromAssemblyInDirectory(new AssemblyFilter("")
                        .FilterByName(x => x.Name.StartsWith("Example."))))
            );
        }
    }
}