Unity Auto Registration (convention over configuration registration)(autoregistration.codeplex.com)
submitted by
Nextender(20) 2 years, 7 months ago
Unity Auto Registration extends Unity IoC container and provides nice fluent syntax to configure rules for automatic types registration. In a couple of code lines you can scan specified assemblies and register all types implementing certain interface or decorated with certain attribute (or satisfy your own predicate) with certain lifetime managers and names.
For example:
var container = new UnityContainer();
container
.ConfigureAutoRegistration()
.LoadAssemblyFrom("Plugin.dll")
.IncludeAllLoadedAssemblies()
.ExcludeSystemAssemblies()
.ExcludeAssemblies(a => a.GetName().FullName.Contains("Test"))
.Include(If.Implements<ILogger>, Then.Register().UsingPerCallMode())
.Include(If.ImplementsITypeName, Then.Register().WithTypeName())
.Include(If.Implements<ICustomerRepository>, Then.Register().WithName("Sample"))
.Include(If.Implements<IOrderRepository>,
Then.Register().AsSingleInterfaceOfType().UsingPerCallMode())
.Include(If.DecoratedWith<LoggerAttribute>,
Then.Register()
.AsInterface<IDisposable>()
.WithTypeName()
.UsingLifetime<MyLifetimeManager>())
.Exclude(t => t.Name.Contains("Trace"))
.ApplyAutoRegistration();
|category: C#
|Views: 145
tags:
C# another
Everyones tags:
Your Tags: