Unity默认提供了ContainerControlledLifetimeManager、TransientLifetimeManager、HierarchicalLifetimeManager、PerResolveLifetimeManager、PerThreadLifetimeManager和ExternallyControlledLifetimeManager。
ContainerControlledLifetimeManager 就是单例模式,在整个AppDomain中类型实例只有一个。
1 IUnityContainer unityContainer = new UnityContainer(); 2 3 unityContainer.RegisterType<TextWriter, StringWriter>(new ContainerControlledLifetimeManager(), new InjectionConstructor()); 4 5 TextWriter textWriter = unityContainer.Resolve<TextWriter>(); 6 TextWriter textWriter2 = unityContainer.Resolve<TextWriter>(); 7 8 Console.WriteLine(Object.ReferenceEquals(textWriter, textWriter2));
TransientLifetimeManager 每次都会创建一个类型的新实例。
1 IUnityContainer unityContainer = new UnityContainer(); 2 3 unityContainer.RegisterType<TextWriter, StringWriter>(new TransientLifetimeManager(), new InjectionConstructor()); 4 5 TextWriter textWriter = unityContainer.Resolve<TextWriter>(); 6 TextWriter textWriter2 = unityContainer.Resolve<TextWriter>(); 7 8 Console.WriteLine(Object.ReferenceEquals(textWriter, textWriter2));
HierarchicalLifetimeManager 单例模式,和ContainerControlledLifetimeManager不同的是子Container和祖先Container不共享类型实例。
1 IUnityContainer unityContainer = new UnityContainer(); 2 IUnityContainer childUnityContainer = unityContainer.CreateChildContainer(); 3 4 unityContainer.RegisterType<TextWriter, StringWriter>(new HierarchicalLifetimeManager(), new InjectionConstructor()); 5 6 TextWriter textWriter = unityContainer.Resolve<TextWriter>(); 7 TextWriter textWriter2 = childUnityContainer.Resolve<TextWriter>();
PerResolveLifetimeManager 和TransientLifetimeManager相同,每次调用Resolve函数都会返回新的类型实例。
1 IUnityContainer unityContainer = new UnityContainer(); 2 IUnityContainer childUnityContainer = unityContainer.CreateChildContainer(); 3 4 unityContainer.RegisterType<TextWriter, StringWriter>(new PerResolveLifetimeManager(), new InjectionConstructor()); 5 6 TextWriter textWriter = unityContainer.Resolve<TextWriter>(); 7 TextWriter textWriter2 = unityContainer.Resolve<TextWriter>(); 8 9 Console.WriteLine(Object.ReferenceEquals(textWriter, textWriter2));
PerThreadLifetimeManager 和ThreadStaticAttribute一样,同一个线程共享一个类型实例。
1 IUnityContainer unityContainer = new UnityContainer(); 2 3 unityContainer.RegisterType<TextWriter, StringWriter>(new PerThreadLifetimeManager(), new InjectionConstructor()); 4 5 TextWriter textWriter = null; 6 TextWriter textWriter2 = null; 7 8 Semaphore semphore = new Semaphore(0, 2); 9 10 new Thread(() => 11 { 12 textWriter = unityContainer.Resolve<TextWriter>(); 13 semphore.Release(); 14 }).Start(); 15 16 new Thread(() => 17 { 18 textWriter2 = unityContainer.Resolve<TextWriter>(); 19 semphore.Release(); 20 }).Start(); 21 22 semphore.WaitOne(); 23 24 Console.WriteLine(Object.ReferenceEquals(textWriter, textWriter2));
ExternallyControlledLifetimeManager 类型共享同一个实例,内部对类型实例weak reference。类型实例的生命周期管理完全交给外部。
1 IUnityContainer unityContainer = new UnityContainer(); 2 3 unityContainer.RegisterType<TextWriter, StringWriter>(new ExternallyControlledLifetimeManager(), new InjectionConstructor()); 4 5 TextWriter textWriter = unityContainer.Resolve<TextWriter>(); 6 TextWriter textWriter2 = unityContainer.Resolve<TextWriter>(); 7 8 Console.WriteLine(Object.ReferenceEquals(textWriter, textWriter2));