public static void RegisterTypesFromAssemblies(this IUnityContainer container, Assembly[] assemblies) { container.RegisterTypes( AllClasses.FromAssemblies(assemblies), WithMappings.FromMatchingInterface, WithName.Default, (instanceType) => { LifetimeManager result = null; var ioc = instanceType.GetCustomAttributes(false).OfType <IOCAttribute>().FirstOrDefault(); if (ioc == null) { result = WithLifetime.Hierarchical(instanceType); } else { switch (ioc.LifeCycle) { case IOCLifeCycleType.Singleton: result = WithLifetime.ContainerControlled(instanceType); break; case IOCLifeCycleType.PerThread: result = WithLifetime.PerThread(instanceType); break; case IOCLifeCycleType.PerResolve: result = WithLifetime.PerResolve(instanceType); break; case IOCLifeCycleType.PerRequest: result = WithLifetime.Hierarchical(instanceType); break; default: result = WithLifetime.Hierarchical(instanceType); break; } } return(result); }); }
public void GetsLifetimeManagers() { Assert.IsInstanceOfType(WithLifetime.ContainerControlled(typeof(MockLogger)), typeof(ContainerControlledLifetimeManager)); Assert.IsInstanceOfType(WithLifetime.ExternallyControlled(typeof(MockLogger)), typeof(ExternallyControlledLifetimeManager)); Assert.IsInstanceOfType(WithLifetime.Hierarchical(typeof(MockLogger)), typeof(HierarchicalLifetimeManager)); Assert.IsNull(WithLifetime.None(typeof(MockLogger))); Assert.IsInstanceOfType(WithLifetime.PerResolve(typeof(MockLogger)), typeof(PerResolveLifetimeManager)); Assert.IsInstanceOfType(WithLifetime.Transient(typeof(MockLogger)), typeof(TransientLifetimeManager)); Assert.IsInstanceOfType(WithLifetime.Custom <CustomLifetimeManager>(typeof(MockLogger)), typeof(CustomLifetimeManager)); #if !NETFX_CORE Assert.IsInstanceOfType(WithLifetime.PerThread(typeof(MockLogger)), typeof(PerThreadLifetimeManager)); #endif }