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 override Func <Type, LifetimeManager> GetLifetimeManager() { var bt = typeof(BaseBiz); return(t => { if (t.IsSubclassOf(bt)) { //return WithLifetime.Transient(t); return WithLifetime.PerResolve(t); } return WithLifetime.ContainerControlled(t); }); }
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 }
public static void All(IUnityContainer c) { if (c == null) { return; } if (Container == null) { Container = c; } List <Assembly> list = AppDomain.CurrentDomain.GetAssemblies().ToList(); foreach (Assembly assembly in list) { IEnumerable <Type> types = from type in GetLoadableTypes(assembly) where Attribute.IsDefined(type, typeof(ImplementAttribute)) select type; foreach (Type to in types) { ImplementAttribute custom = to.GetCustomAttributes <ImplementAttribute>().SingleOrDefault(); if (custom != null) { Container.RegisterType(custom.FromType, to, WithName.Default(to), WithLifetime.ContainerControlled(to)); } } } }