Created
July 6, 2016 09:02
-
-
Save westonal/c1e1e79cb9ee971ac1ba07b7f8e3ce36 to your computer and use it in GitHub Desktop.
Demo of Child containers and hierarchical lifetimes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Microsoft.Practices.Unity; | |
| using NUnit.Framework; | |
| namespace UnityDemo | |
| { | |
| [TestFixture] | |
| public sealed class ChildContainers | |
| { | |
| [Test] | |
| public void Single_container() | |
| { | |
| var root = new UnityContainer(); | |
| root.RegisterType<IService, Service>(); | |
| root.RegisterType<IDependancy, Dependancy1>(); | |
| var service = root.Resolve<IService>(); | |
| Assert.AreEqual("Service using 1", service.ToString()); | |
| } | |
| [Test] | |
| public void A_single_child_container() | |
| { | |
| var root = new UnityContainer(); | |
| root.RegisterType<IService, Service>(); | |
| IUnityContainer child1 = root.CreateChildContainer(); | |
| child1.RegisterType<IDependancy, Dependancy1>(); | |
| var service = child1.Resolve<IService>(); | |
| Assert.AreEqual("Service using 1", service.ToString()); | |
| } | |
| [Test] | |
| public void Two_child_containers() | |
| { | |
| var root = new UnityContainer(); | |
| root.RegisterType<IService, Service>(); | |
| IUnityContainer child1 = root.CreateChildContainer(); | |
| child1.RegisterType<IDependancy, Dependancy1>(); | |
| IUnityContainer child2 = root.CreateChildContainer(); | |
| child2.RegisterType<IDependancy, Dependancy2>(); | |
| var service1 = child1.Resolve<IService>(); | |
| Assert.AreEqual("Service using 1", service1.ToString()); | |
| var service2 = child2.Resolve<IService>(); | |
| Assert.AreEqual("Service using 2", service2.ToString()); | |
| } | |
| [Test] | |
| public void Without_heirachical_lifetimes() | |
| { | |
| var root = new UnityContainer(); | |
| root.RegisterType<IService, Service>(); | |
| IUnityContainer child1 = root.CreateChildContainer(); | |
| child1.RegisterType<IDependancy, Dependancy1>(); | |
| Assert.AreNotSame(child1.Resolve<IService>(), child1.Resolve<IService>()); | |
| } | |
| [Test] | |
| public void Singleton() | |
| { | |
| var root = new UnityContainer(); | |
| root.RegisterType<IDependancy, Dependancy1>(new ContainerControlledLifetimeManager()); | |
| Assert.AreSame(root.Resolve<IDependancy>(), root.Resolve<IDependancy>()); | |
| } | |
| [Test] | |
| public void With_heirachical_lifetimes() | |
| { | |
| var root = new UnityContainer(); | |
| root.RegisterType<IService, Service>(new HierarchicalLifetimeManager()); | |
| IUnityContainer child1 = root.CreateChildContainer(); | |
| child1.RegisterType<IDependancy, Dependancy1>(); | |
| IUnityContainer child2 = root.CreateChildContainer(); | |
| child2.RegisterType<IDependancy, Dependancy2>(); | |
| Assert.AreSame(child1.Resolve<IService>(), child1.Resolve<IService>()); | |
| Assert.AreSame(child2.Resolve<IService>(), child2.Resolve<IService>()); | |
| Assert.AreNotSame(child1.Resolve<IService>(), child2.Resolve<IService>()); | |
| } | |
| [Test] | |
| public void With_named_containers() | |
| { | |
| var root = new UnityContainer(); | |
| root.RegisterType<IService, Service>(new HierarchicalLifetimeManager()); | |
| RegisterWers(RegisterNewChildContainerNamed(root, "wers")); | |
| RegisterUsc(RegisterNewChildContainerNamed(root, "usc")); | |
| var wers = root.Resolve<IUnityContainer>("wers"); | |
| Assert.AreEqual("Service using 1",wers.Resolve<IService>().ToString()); | |
| var usc = root.Resolve<IUnityContainer>("usc"); | |
| Assert.AreEqual("Service using 2", usc.Resolve<IService>().ToString()); | |
| } | |
| private static void RegisterUsc(IUnityContainer container) | |
| { | |
| container.RegisterType<IDependancy, Dependancy2>(); | |
| } | |
| private static void RegisterWers(IUnityContainer container) | |
| { | |
| container.RegisterType<IDependancy, Dependancy1>(); | |
| } | |
| private static IUnityContainer RegisterNewChildContainerNamed(IUnityContainer root, string name) | |
| { | |
| IUnityContainer childContainer = root.CreateChildContainer(); | |
| root.RegisterInstance(name, childContainer); | |
| return childContainer; | |
| } | |
| } | |
| public interface IService | |
| { | |
| } | |
| public class Service : IService | |
| { | |
| private readonly IDependancy _dependancy; | |
| public Service(IDependancy dependancy) | |
| { | |
| _dependancy = dependancy; | |
| } | |
| public override string ToString() | |
| { | |
| return "Service using " + _dependancy; | |
| } | |
| } | |
| public interface IDependancy | |
| { | |
| } | |
| public class Dependancy1 : IDependancy | |
| { | |
| public override string ToString() | |
| { | |
| return "1"; | |
| } | |
| } | |
| public class Dependancy2 : IDependancy | |
| { | |
| public override string ToString() | |
| { | |
| return "2"; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment