public void DependencyOverrideWithNestedContainer() { IToWhichDependent defaultValue = new Type1ToWhichDependent(111); IToWhichDependent overrideValue3 = new Type2ToWhichDependent(333); IToWhichDependent overrideValue4 = new Type2ToWhichDependent(444); IUnityContainer container = new UnityContainer(); IUnityContainer childContainer = container.CreateChildContainer(); // Registering the parent default types. container.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)); // Registering the child default types. childContainer.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(222)); // Overriding the default values for the parent container. Type1DependingOnOtherType parentResult = (Type1DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue3)); // Overriding the default values for the child container. Type1DependingOnOtherType childResult = (Type1DependingOnOtherType)childContainer.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue4)); // The parent overriden values should be reflected. Assert.IsInstanceOfType(parentResult.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(parentResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual <int>(parentResult.IToWhichDependent.X, 333); // The child overriden values should be reflected. Assert.IsInstanceOfType(childResult.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(childResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual <int>(childResult.IToWhichDependent.X, 444); }
public void NestedContainerOverrideInParentDoesNotReturnOverridenInChildAcrossCalls() { IToWhichDependent overrideValue = new Type2ToWhichDependent(222); IUnityContainer container = new UnityContainer(); IUnityContainer childContainer = container.CreateChildContainer(); // Registering the parent default types. container.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)); // Overriding the default values for the parent container. Type1DependingOnOtherType parentResult = (Type1DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue)); // Resolving child container to the default type. Type1DependingOnOtherType childResult = (Type1DependingOnOtherType)childContainer.Resolve <IDependingOnOtherType>(); // The parent overriden values should be reflected. Assert.IsInstanceOfType(parentResult.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(parentResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual <int>(parentResult.IToWhichDependent.X, 222); // The parent default registered type should be reflected, since there are no type registered exclusively for the child container. Assert.IsInstanceOfType(childResult.IToWhichDependent, typeof(Type1ToWhichDependent)); Assert.IsInstanceOfType(childResult.OneMoreIToWhichDependent, typeof(Type1ToWhichDependent)); Assert.AreEqual <int>(childResult.IToWhichDependent.X, 111); }
public void DependencyOverridesTypeMismatchTest() { IToWhichDependent defaultValue = new Type1ToWhichDependent(111); IToWhichDependent overrideValue = new Type2ToWhichDependent(222); IUnityContainer container = new UnityContainer(); container.RegisterType<IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType<IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)); Type1DependingOnOtherType result = (Type1DependingOnOtherType)container.Resolve<IDependingOnOtherType>(new DependencyOverride(typeof(int), overrideValue)); }
public void DependencyOverridesTypeMismatchTest() { IToWhichDependent defaultValue = new Type1ToWhichDependent(111); IToWhichDependent overrideValue = new Type2ToWhichDependent(222); IUnityContainer container = new UnityContainer(); container.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)); Type1DependingOnOtherType result = (Type1DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(int), overrideValue)); }
public void DependencyOverrideWithConstructorDependency() { // Using the [Dependency] attribute in to the constructor parameter. IToWhichDependent overrideValue = new Type2ToWhichDependent(222); IUnityContainer container = new UnityContainer(); container.RegisterType<IDependingOnOtherType, Type3DependingOnOtherType>().RegisterType<IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(999)); Type3DependingOnOtherType result = (Type3DependingOnOtherType)container.Resolve<IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue)); Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); }
public void DependencyOverridesAllMatchTest() { IToWhichDependent defaultValue = new Type1ToWhichDependent(111); IToWhichDependent overrideValue = new Type2ToWhichDependent(222); IUnityContainer container = new UnityContainer(); container.RegisterType<IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType<IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)); Type1DependingOnOtherType result = (Type1DependingOnOtherType)container.Resolve<IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue)); Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); }
public void DependencyOverrideWithConstructorDependency() { // Using the [Dependency] attribute in to the constructor parameter. IToWhichDependent overrideValue = new Type2ToWhichDependent(222); IUnityContainer container = new UnityContainer(); container.RegisterType <IDependingOnOtherType, Type3DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(999)); Type3DependingOnOtherType result = (Type3DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue)); Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); }
public void DependencyOverridesAllMatchTest() { IToWhichDependent defaultValue = new Type1ToWhichDependent(111); IToWhichDependent overrideValue = new Type2ToWhichDependent(222); IUnityContainer container = new UnityContainer(); container.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)); Type1DependingOnOtherType result = (Type1DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue)); Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); }
public void DependencyOverrideValueInConfigFile() { IToWhichDependent overrideDependency = new Type2ToWhichDependent(9999); DependencyOverride <IToWhichDependent> overrideParam = new DependencyOverride <IToWhichDependent>(overrideDependency); IUnityContainer container = GetContainer("DependencyOverrideContainer"); var result = container.Resolve <Type3DependingOnOtherType>("TestDependencyOverrideDefaultInConfiguration", overrideParam); Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual <int>(9999, result.OneMoreIToWhichDependent.X); Assert.AreEqual <int>(9999, result.IToWhichDependent.X); }
public void WhenOverridingMultipleDependencies() { IUnityContainer container = new UnityContainer(); container.RegisterType <Type4DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)).RegisterType <Dependent>(new InjectionProperty("X", 111)); Dependent overrideDependent1 = new Dependent(); overrideDependent1.X = 9999; IToWhichDependent overrideDependent2 = new Type2ToWhichDependent(8888); DependencyOverride <Dependent> overrideParam1 = new DependencyOverride <Dependent>(overrideDependent1); DependencyOverride <IToWhichDependent> overrideParam2 = new DependencyOverride <IToWhichDependent>(overrideDependent2); var result = container.Resolve <Type4DependingOnOtherType>(overrideParam1, overrideParam2); Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual <int>(8888, result.IToWhichDependent.X); Assert.AreEqual <int>(8888, result.OneMoreIToWhichDependent.X); Assert.AreEqual <int>(9999, result.NewToWhichDependent.X); }
public void NestedContainerOverrideInParentDoesNotReturnOverridenInChildAcrossCalls() { IToWhichDependent overrideValue = new Type2ToWhichDependent(222); IUnityContainer container = new UnityContainer(); IUnityContainer childContainer = container.CreateChildContainer(); // Registering the parent default types. container.RegisterType<IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType<IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)); // Overriding the default values for the parent container. Type1DependingOnOtherType parentResult = (Type1DependingOnOtherType)container.Resolve<IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue)); // Resolving child container to the default type. Type1DependingOnOtherType childResult = (Type1DependingOnOtherType)childContainer.Resolve<IDependingOnOtherType>(); // The parent overriden values should be reflected. Assert.IsInstanceOfType(parentResult.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(parentResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual<int>(parentResult.IToWhichDependent.X, 222); // The parent default registered type should be reflected, since there are no type registered exclusively for the child container. Assert.IsInstanceOfType(childResult.IToWhichDependent, typeof(Type1ToWhichDependent)); Assert.IsInstanceOfType(childResult.OneMoreIToWhichDependent, typeof(Type1ToWhichDependent)); Assert.AreEqual<int>(childResult.IToWhichDependent.X, 111); }
public void WhenOverridingMultipleDependencies() { IUnityContainer container = new UnityContainer(); container.RegisterType<Type4DependingOnOtherType>().RegisterType<IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)).RegisterType<Dependent>(new InjectionProperty("X", 111)); Dependent overrideDependent1 = new Dependent(); overrideDependent1.X = 9999; IToWhichDependent overrideDependent2 = new Type2ToWhichDependent(8888); DependencyOverride<Dependent> overrideParam1 = new DependencyOverride<Dependent>(overrideDependent1); DependencyOverride<IToWhichDependent> overrideParam2 = new DependencyOverride<IToWhichDependent>(overrideDependent2); var result = container.Resolve<Type4DependingOnOtherType>(overrideParam1, overrideParam2); Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual<int>(8888, result.IToWhichDependent.X); Assert.AreEqual<int>(8888, result.OneMoreIToWhichDependent.X); Assert.AreEqual<int>(9999, result.NewToWhichDependent.X); }
public void DependencyOverrideValueInConfigFileDoesNotPersistAcrossCalls() { IToWhichDependent overrideDependency = new Type2ToWhichDependent(9999); DependencyOverride<IToWhichDependent> overrideParam = new DependencyOverride<IToWhichDependent>(overrideDependency); IUnityContainer container = GetContainer("DependencyOverrideContainer"); var result = container.Resolve<Type3DependingOnOtherType>("TestDependencyOverrideDefaultInConfiguration", overrideParam); var defaultResult = container.Resolve<Type3DependingOnOtherType>("TestDependencyOverrideDefaultInConfiguration"); Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual<int>(9999, result.OneMoreIToWhichDependent.X); Assert.AreEqual<int>(9999, result.IToWhichDependent.X); Assert.IsInstanceOfType(defaultResult.IToWhichDependent, typeof(Type1ToWhichDependent)); Assert.IsInstanceOfType(defaultResult.OneMoreIToWhichDependent, typeof(Type1ToWhichDependent)); Assert.AreEqual<int>(-111, defaultResult.OneMoreIToWhichDependent.X); Assert.AreEqual<int>(-111, defaultResult.IToWhichDependent.X); }
public void DependencyOverrideWithNestedContainer() { IToWhichDependent defaultValue = new Type1ToWhichDependent(111); IToWhichDependent overrideValue3 = new Type2ToWhichDependent(333); IToWhichDependent overrideValue4 = new Type2ToWhichDependent(444); IUnityContainer container = new UnityContainer(); IUnityContainer childContainer = container.CreateChildContainer(); // Registering the parent default types. container.RegisterType<IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType<IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111)); // Registering the child default types. childContainer.RegisterType<IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType<IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(222)); // Overriding the default values for the parent container. Type1DependingOnOtherType parentResult = (Type1DependingOnOtherType)container.Resolve<IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue3)); // Overriding the default values for the child container. Type1DependingOnOtherType childResult = (Type1DependingOnOtherType)childContainer.Resolve<IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue4)); // The parent overriden values should be reflected. Assert.IsInstanceOfType(parentResult.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(parentResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual<int>(parentResult.IToWhichDependent.X, 333); // The child overriden values should be reflected. Assert.IsInstanceOfType(childResult.IToWhichDependent, typeof(Type2ToWhichDependent)); Assert.IsInstanceOfType(childResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent)); Assert.AreEqual<int>(childResult.IToWhichDependent.X, 444); }