From 6c93833399f7202dfd9cb39e1551c13e0730ad1d Mon Sep 17 00:00:00 2001 From: Stefan Moser Date: Wed, 20 Jan 2016 08:22:30 -0800 Subject: [PATCH] implement UnregisterMultiple --- src/TinyIoC.Tests/TinyIoCTests.cs | 72 +++++++++++++++++++++++++++++++ src/TinyIoC/TinyIoC.cs | 24 +++++++++++ 2 files changed, 96 insertions(+) diff --git a/src/TinyIoC.Tests/TinyIoCTests.cs b/src/TinyIoC.Tests/TinyIoCTests.cs index d565f59..69af395 100644 --- a/src/TinyIoC.Tests/TinyIoCTests.cs +++ b/src/TinyIoC.Tests/TinyIoCTests.cs @@ -3481,6 +3481,78 @@ public void Unregister_T_NotRegisteredNamedInterface_CannotUnregister() #endregion + #region Unregister Multiple + + [TestMethod] + public void UnregisterMultiple_T_ValidTypes_CorrectCountReturnedByResolveAll() + { + var container = UtilityMethods.GetContainer(); + container.RegisterMultiple(new Type[] { typeof(TestClassDefaultCtor), typeof(DisposableTestClassWithInterface) }); + + container.UnregisterMultiple(new Type[] {typeof(TestClassDefaultCtor)}); + var result = container.ResolveAll(); + + Assert.AreEqual(1, result.Count()); + } + + [TestMethod] + public void UnregisterMultiple_ValidTypes_CorrectCountReturnedByResolveAll() + { + var container = UtilityMethods.GetContainer(); + container.RegisterMultiple(typeof(ITestInterface), new Type[] { typeof(TestClassDefaultCtor), typeof(DisposableTestClassWithInterface) }); + + container.UnregisterMultiple(typeof(ITestInterface), new Type[] {typeof(TestClassDefaultCtor)}); + var result = container.ResolveAll(); + + Assert.AreEqual(1, result.Count()); + } + + [TestMethod] + public void UnregisterMultiple_T_ValidTypes_InstancesOfCorrectTypesReturnedByResolveAll() + { + var container = UtilityMethods.GetContainer(); + container.RegisterMultiple(new Type[] { typeof(TestClassDefaultCtor), typeof(DisposableTestClassWithInterface) }); + + container.UnregisterMultiple(new Type[] { typeof(TestClassDefaultCtor) }); + var result = container.ResolveAll(); + + Assert.IsNotNull(result.Where(o => o.GetType() == typeof(DisposableTestClassWithInterface)).FirstOrDefault()); + } + + [TestMethod] + public void UnregisterMultiple_T_Null_Throws() + { + var container = UtilityMethods.GetContainer(); + + try + { + container.UnregisterMultiple(null); + + Assert.Fail(); + } + catch (ArgumentNullException) + { + } + } + + [TestMethod] + public void UnregisterMultiple_T_ATypeThatDoesntImplementTheRegisterType_Throws() + { + var container = UtilityMethods.GetContainer(); + + try + { + container.UnregisterMultiple(new Type[] { typeof(TestClassDefaultCtor), typeof(TestClass2) }); + + Assert.Fail(); + } + catch (ArgumentException) + { + } + } + + #endregion + #endregion } } diff --git a/src/TinyIoC/TinyIoC.cs b/src/TinyIoC/TinyIoC.cs index b073983..ba179ef 100644 --- a/src/TinyIoC/TinyIoC.cs +++ b/src/TinyIoC/TinyIoC.cs @@ -1650,6 +1650,30 @@ public bool Unregister(Type registerType, string name) return RemoveRegistration(typeRegistration); } + public void UnregisterMultiple(Type registrationType, IEnumerable implementationTypes) + { + if (implementationTypes == null) + throw new ArgumentNullException("implementationTypes", "types is null."); + + foreach (var type in implementationTypes) +//#if NETFX_CORE +// if (!registrationType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) +//#else + if (!registrationType.IsAssignableFrom(type)) +//#endif + throw new ArgumentException(String.Format("types: The type {0} is not assignable from {1}", registrationType.FullName, type.FullName)); + + foreach (var type in implementationTypes) + { + Unregister(registrationType, type.FullName); + } + } + + public void UnregisterMultiple(IEnumerable implementationTypes) + { + UnregisterMultiple(typeof(UnregisterType), implementationTypes); + } + #endregion #region Resolution