Auto Registration in Microsoft Unity

Unlike many other .NET IoC containers, Microsoft Unity does not support auto registrations. Auto registration is a very useful feature for any IoC container and it would be very helpful in some specific contexts.  The Unity Auto Registration project in Codeplex will provide the support for automatic registration using a fluent API. You can add Unity Auto Registration package using NuGet. The following command will add reference to Unity Auto Registration.

PM> Install-Package UnityAutoRegistration

The following registration syntax will scan the all assemblies except system assemblies and will register all types that implemented the open generic type ICommandHandler<> 

  1. IUnityContainer container = new UnityContainer();
  2. container.ConfigureAutoRegistration()
  3.     .ExcludeSystemAssemblies()   
  4. .Include(type => type.ImplementsOpenGeneric(typeof(ICommandHandler<>)),
  5.     Then.Register().AsFirstInterfaceOfType().WithTypeName())
  6. .ApplyAutoRegistration();

 

No Comments