public LifetimeManager CreateLifeTimeManager(LifeTimeManagerType type) { LifetimeManager result = null; switch (type) { case LifeTimeManagerType.ContainerControlled: result = new ContainerControlledLifetimeManager(); break; case LifeTimeManagerType.HierarchicalLifetime: result = new HierarchicalLifetimeManager(); break; case LifeTimeManagerType.PerResolveLifetime: result = new PerResolveLifetimeManager(); break; case LifeTimeManagerType.ExternallyControlledLifetime: result = new ExternallyControlledLifetimeManager(); break; case LifeTimeManagerType.PerThreadLifetime: result = new PerThreadLifetimeManager(); break; case LifeTimeManagerType.TransientLifetime: result = new TransientLifetimeManager(); break; } return result; }
private static ILifetimePolicy GetLifetimePolicy(IBuilderContext context) { ILifetimePolicy policy = context.Policies.GetNoDefault<ILifetimePolicy>(context.BuildKey, false); if(policy == null && context.BuildKey.Type.IsGenericType) { policy = GetLifetimePolicyForGenericType(context); } if(policy == null) { policy = new TransientLifetimeManager(); context.PersistentPolicies.Set<ILifetimePolicy>(policy, context.BuildKey); } return policy; }
public static void Register(this IUnityContainer container, IServiceDescriptor serviceDescriptor) { LifetimeManager lifetimeManager = null; switch (serviceDescriptor.Lifecycle) { case LifecycleKind.Singleton: lifetimeManager = new ContainerControlledLifetimeManager(); break; case LifecycleKind.Scoped: lifetimeManager = new HierarchicalLifetimeManager(); break; case LifecycleKind.Transient: lifetimeManager = new TransientLifetimeManager(); break; } if (serviceDescriptor.ImplementationInstance != null) container.RegisterInstance(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationInstance); else container.RegisterType(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationType, lifetimeManager); }
static void TestTransientLifetime_Nested() { Console.WriteLine("Test TransientLifetimeManager_Nested"); LifetimeTest.ResetCounter(); using (var container = new UnityContainer()) { var lifeManager = new TransientLifetimeManager(); container.RegisterType<ILifetimeTest, LifetimeTest>(lifeManager); var obj1 = container.Resolve<Foo>(); LifetimeTest.PrintCounter(); } LifetimeTest.PrintCounter(); }
// DependencyFactory.RegisterDependency(ProjectType.Wcf, "MT.Complex"); public static void RegisterDependency(ProjectType projecttype, string projectname) { string DllPath = string.Empty; switch ( projecttype ) { case ProjectType.Web: DllPath = AppDomain.CurrentDomain.BaseDirectory + "\\bin\\" + projectname + ".dll"; break; case ProjectType.Winfom: case ProjectType.WPF: case ProjectType.Wcf: case ProjectType.Test: DllPath = AppDomain.CurrentDomain.BaseDirectory + "\\" + projectname + ".dll"; break; } if ( DllPath == null || DllPath.Length == 0 ) { throw new Exception("无法解析项目DLL"); } var typeList = Assembly.LoadFrom(DllPath).GetTypes().Where(t => t.Namespace != null && t.Namespace.Contains("Realization") && t.IsInterface == false && t.IsAbstract == false); var LifetimeManagerRegisterlist = typeList.Where(t => { return t.GetInterfaces().Intersect(ILifetimeManagerRegisterList).Count() > 0; }); foreach ( var t in LifetimeManagerRegisterlist ) { var InterfaceList = t.GetInterfaces().Where(p => { return !ILifetimeManagerRegisterList.Contains(p) && p.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null; }); LifetimeManager lifetimemanager = new TransientLifetimeManager(); var intertype = t.GetInterfaces().Intersect(ILifetimeManagerRegisterList).First(); switch ( intertype.Name ) { case "IContainerControlledLifetimeManagerRegister": lifetimemanager = new ContainerControlledLifetimeManager(); break; case "IHierarchicalLifetimeManagerRegister": lifetimemanager = new HierarchicalLifetimeManager(); break; case "IExternallyControlledLifetimeManagerRegister": lifetimemanager = new ExternallyControlledLifetimeManager(); break; case "IPerThreadLifetimeManagerRegister": lifetimemanager = new PerThreadLifetimeManager(); break; case "IPerResolveLifetimeManagerRegister": lifetimemanager = new PerResolveLifetimeManager(); break; } foreach ( var iType in InterfaceList ) { ICOConfigAttribute ds = (ICOConfigAttribute) t.GetCustomAttribute(typeof(ICOConfigAttribute), false); ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute) iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false); if ( ia.AOPEnable ) { var generator = new DynamicProxyGenerator(t, iType); Type type = generator.GenerateType(); // Type type = typeof(TransientLifetimeManager); DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, lifetimemanager); } else { DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, lifetimemanager); } } } }
/// <summary> /// Registers the type. /// </summary> /// <param name="serviceType">Type of the service.</param> /// <param name="implementationType">Type of the implementation.</param> /// <param name="lifetime">The lifetime of the service.</param> /// <returns></returns> public override IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime) { Invariant.IsNotNull(serviceType, "serviceType"); Invariant.IsNotNull(implementationType, "implementationType"); LifetimeManager lifeTimeManager; switch (lifetime) { case LifetimeType.PerRequest: lifeTimeManager = new PerRequestLifetimeManager(); break; case LifetimeType.Singleton: lifeTimeManager = new ContainerControlledLifetimeManager(); break; default: lifeTimeManager = new TransientLifetimeManager(); break; } if (Container.Registrations.Any(registration => registration.RegisteredType.Equals(serviceType))) { Container.RegisterType(serviceType, implementationType, implementationType.FullName, lifeTimeManager); } else { Container.RegisterType(serviceType, implementationType, lifeTimeManager); } return this; }