Convention based configuration for Microsoft Unity

In a previous project, long story short, we used the Microsoft Unity IoC container. After writing line upon line with configuration statements while looking at the nice convention based configuration options for other containers, I decided that enough was enough. Replacing the Unity container was sadly not an option at the moment, so I went ahead and created a convention based configuration api for Unity.

Below you can see a small example of configuring a container using registries, and I should probably apologize for the poor (or rather lack of) documentation at this time.

You get access to the configuration api by using the extension method Configure on the IUnityContainer interface.

1 var container = new UnityContainer();
2 container.Configure(x =>
3 {
4   x.AddRegistry<FooRegistry>();
5   x.AddRegistry<BarRegistry>();
6 });

Configuration is done in one or several registries that inherit the UnityRegistry base class.

 1 public class FooRegistry : UnityRegistry
 2 {
 3   public FooRegistry()
 4   {
 5     // Scan one or several assemblies and auto register types based on a
 6     // convention. You can also include or exclude certain types and/or
 7     // namespaces using a filter.
 8     Scan(scan =>
 9     {
10       scan.AssemblyContaining<FooRegistry>();
11       scan.With<FirstInterfaceConvention>();
12       scan.With<AddAllConvention>().TypesImplementing<IHaveManyImplementations>();
13       scan.With<SetAllPropertiesConvention>().OfType<ILogger>();
14       scan.ExcludeType<FooService>();
15     });
17     // Manually register a service
18     Register<IFooService, FooService>().WithName("Foo").AsSingleton();
20     // Make services a singleton
21     MakeSingleton<ISingletonService>();
23     // You can automatically configure the container to call
24     // a method on any service when they are created
25     AfterBuildingUp<IStartable>().Call((c, s) => s.Start();
27     // You can automatically configure the container to
28     // decorate services when they are created
29     AfterBuildingUp<IFooService>().DecorateWith((c, t) => new FooDecorator(t));
31     // If you want to inject values or objects that are not registered in
32     // the container, you can do so by adding them using this statement.
33     // For instances you want the container to create, just specify the type.
34     ConfigureCtorArgsFor<ServiceWithCtorArgs>("some string", typeof (IFooService));
36     // Unity follows the greedy constructor pattern when creating instances.
37     // If you want to use a different constructor, you specify it by listing
38     // the types of the arguments in the constructor you want it to use.
39     SelectConstructor<ServiceWithCtorArgs>(typeof (IFooService));
40   }
41 }

At the moment, built in conventions includes AddAllConvention, FirstInterfaceConvention and SetAllPropertiesConvention. If these doesn't suit you, creating custom conventions is as easy as creating a class that implements the IAssemblyScanner interface.

1 public class CustomConvention : IAssemblyScannerConvention
2 {
3   void IAssemblyScannerConvention.Process(Type type, IUnityRegistry registry)
4   {
5     if (type == typeof(FooService))
6       registry.Register<IFooService, FooService>().WithName("Custom");
7   }
8 }


The framework is available as a NuGet package, and can be installed either using the package manager console in Visual Studio like this:

1    PM> Install-Package UnityConfiguration

Or by right clicking on references in your project and select the Add Library Package Reference option


The source code for this project is licensed under the MIT License and is available on my GitHub profile.