Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement UnregisterMultiple #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/TinyIoC.Tests/TinyIoCTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITestInterface>(new Type[] { typeof(TestClassDefaultCtor), typeof(DisposableTestClassWithInterface) });

container.UnregisterMultiple<ITestInterface>(new Type[] {typeof(TestClassDefaultCtor)});
var result = container.ResolveAll<ITestInterface>();

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<ITestInterface>();

Assert.AreEqual(1, result.Count());
}

[TestMethod]
public void UnregisterMultiple_T_ValidTypes_InstancesOfCorrectTypesReturnedByResolveAll()
{
var container = UtilityMethods.GetContainer();
container.RegisterMultiple<ITestInterface>(new Type[] { typeof(TestClassDefaultCtor), typeof(DisposableTestClassWithInterface) });

container.UnregisterMultiple<ITestInterface>(new Type[] { typeof(TestClassDefaultCtor) });
var result = container.ResolveAll<ITestInterface>();

Assert.IsNotNull(result.Where(o => o.GetType() == typeof(DisposableTestClassWithInterface)).FirstOrDefault());
}

[TestMethod]
public void UnregisterMultiple_T_Null_Throws()
{
var container = UtilityMethods.GetContainer();

try
{
container.UnregisterMultiple<ITestInterface>(null);

Assert.Fail();
}
catch (ArgumentNullException)
{
}
}

[TestMethod]
public void UnregisterMultiple_T_ATypeThatDoesntImplementTheRegisterType_Throws()
{
var container = UtilityMethods.GetContainer();

try
{
container.UnregisterMultiple<ITestInterface>(new Type[] { typeof(TestClassDefaultCtor), typeof(TestClass2) });

Assert.Fail();
}
catch (ArgumentException)
{
}
}

#endregion

#endregion
}
}
24 changes: 24 additions & 0 deletions src/TinyIoC/TinyIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,30 @@ public bool Unregister(Type registerType, string name)
return RemoveRegistration(typeRegistration);
}

public void UnregisterMultiple(Type registrationType, IEnumerable<Type> 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure about including the commented out #if NETFX_CORE. This code was copied from the implementation of public MultiRegisterOptions RegisterMultiple(Type registrationType, IEnumerable<Type> implementationTypes) so I left it in for consistency.

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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key line of code. It implements the same type.FullName convention that RegisterMultiple does.

}
}

public void UnregisterMultiple<UnregisterType>(IEnumerable<Type> implementationTypes)
{
UnregisterMultiple(typeof(UnregisterType), implementationTypes);
}

#endregion

#region Resolution
Expand Down