Skip to content

Commit

Permalink
Merge pull request #67 from marcosbozzani/Unregister
Browse files Browse the repository at this point in the history
Added Unregister methods
  • Loading branch information
grumpydev committed May 28, 2015
2 parents de91aec + 38147fb commit 41ca742
Show file tree
Hide file tree
Showing 2 changed files with 268 additions and 2 deletions.
218 changes: 218 additions & 0 deletions src/TinyIoC.Tests/TinyIoCTests.cs
Expand Up @@ -3264,5 +3264,223 @@ public void Resolve_RegisteredOpenGeneric_ReturnsInstance()
Assert.IsInstanceOfType(result, typeof(DefaultThing<object>));
}
#endif

#region Unregister

private readonly ResolveOptions options = ResolveOptions.FailUnregisteredAndNameNotFound;

#region Unregister With Implementation

[TestMethod]
public void Unregister_RegisteredImplementation_CanUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<TestClassDefaultCtor>();

bool unregistered = container.Unregister(typeof(TestClassDefaultCtor));
bool resolved = container.CanResolve<TestClassDefaultCtor>(options);

Assert.IsTrue(unregistered);
Assert.IsFalse(resolved);
}

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

bool unregistered = container.Unregister(typeof(TestClassDefaultCtor));

Assert.IsFalse(unregistered);
}

[TestMethod]
public void Unregister_RegisteredNamedImplementation_CanUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<TestClassDefaultCtor>("TestName");

bool unregistered = container.Unregister(typeof(TestClassDefaultCtor), "TestName");
bool resolved = container.CanResolve<TestClassDefaultCtor>("TestName", options);

Assert.IsTrue(unregistered);
Assert.IsFalse(resolved);
}

[TestMethod]
public void Unregister_NotRegisteredNamedImplementation_CannotUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<TestClassDefaultCtor>("TestName");

bool unregistered = container.Unregister(typeof(TestClassDefaultCtor), "UnregisteredName");
bool resolved = container.CanResolve<TestClassDefaultCtor>("TestName", options);

Assert.IsFalse(unregistered);
Assert.IsTrue(resolved);
}

#endregion

#region Unregister With Interface

[TestMethod]
public void Unregister_RegisteredInterface_CanUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<ITestInterface, TestClassDefaultCtor>();

bool unregistered = container.Unregister(typeof(ITestInterface));
bool resolved = container.CanResolve<ITestInterface>(options);

Assert.IsTrue(unregistered);
Assert.IsFalse(resolved);
}

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

bool unregistered = container.Unregister(typeof(ITestInterface));

Assert.IsFalse(unregistered);
}

[TestMethod]
public void Unregister_RegisteredNamedInterface_CanUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<ITestInterface, TestClassDefaultCtor>("TestName");

bool unregistered = container.Unregister(typeof(ITestInterface), "TestName");
bool resolved = container.CanResolve<ITestInterface>("TestName", options);

Assert.IsTrue(unregistered);
Assert.IsFalse(resolved);
}

[TestMethod]
public void Unregister_NotRegisteredNamedInterface_CannotUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<ITestInterface, TestClassDefaultCtor>("TestName");

bool unregistered = container.Unregister(typeof(ITestInterface), "UnregisteredName");
bool resolved = container.CanResolve<ITestInterface>("TestName", options);

Assert.IsFalse(unregistered);
Assert.IsTrue(resolved);
}

#endregion

#region Unregister<T> With Implementation

[TestMethod]
public void Unregister_T_RegisteredImplementation_CanUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<TestClassDefaultCtor>();

bool unregistered = container.Unregister<TestClassDefaultCtor>();
bool resolved = container.CanResolve<TestClassDefaultCtor>(options);

Assert.IsTrue(unregistered);
Assert.IsFalse(resolved);
}

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

bool unregistered = container.Unregister<TestClassDefaultCtor>();

Assert.IsFalse(unregistered);
}

[TestMethod]
public void Unregister_T_RegisteredNamedImplementation_CanUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<TestClassDefaultCtor>("TestName");

bool unregistered = container.Unregister<TestClassDefaultCtor>("TestName");
bool resolved = container.CanResolve<TestClassDefaultCtor>("TestName", options);

Assert.IsTrue(unregistered);
Assert.IsFalse(resolved);
}

[TestMethod]
public void Unregister_T_NotRegisteredNamedImplementation_CannotUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<TestClassDefaultCtor>("TestName");

bool unregistered = container.Unregister<TestClassDefaultCtor>("UnregisteredName");
bool resolved = container.CanResolve<TestClassDefaultCtor>("TestName", options);

Assert.IsFalse(unregistered);
Assert.IsTrue(resolved);
}

#endregion

#region Unregister<T> With Interface

[TestMethod]
public void Unregister_T_RegisteredInterface_CanUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<ITestInterface, TestClassDefaultCtor>();

bool unregistered = container.Unregister<ITestInterface>();
bool resolved = container.CanResolve<ITestInterface>(options);

Assert.IsTrue(unregistered);
Assert.IsFalse(resolved);
}

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

bool unregistered = container.Unregister<ITestInterface>();

Assert.IsFalse(unregistered);
}

[TestMethod]
public void Unregister_T_RegisteredNamedInterface_CanUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<ITestInterface, TestClassDefaultCtor>("TestName");

bool unregistered = container.Unregister<ITestInterface>("TestName");
bool resolved = container.CanResolve<ITestInterface>("TestName", options);

Assert.IsTrue(unregistered);
Assert.IsFalse(resolved);
}

[TestMethod]
public void Unregister_T_NotRegisteredNamedInterface_CannotUnregister()
{
var container = UtilityMethods.GetContainer();
container.Register<ITestInterface, TestClassDefaultCtor>("TestName");

bool unregistered = container.Unregister<ITestInterface>("UnregisteredName");
bool resolved = container.CanResolve<ITestInterface>("TestName", options);

Assert.IsFalse(unregistered);
Assert.IsTrue(resolved);
}

#endregion

#endregion
}
}
52 changes: 50 additions & 2 deletions src/TinyIoC/TinyIoC.cs
Expand Up @@ -1561,6 +1561,54 @@ into j

return new MultiRegisterOptions(registerOptions);
}
#endregion

#region Unregistration

/// <summary>
/// Remove a container class registration.
/// </summary>
/// <typeparam name="RegisterType">Type to unregister</typeparam>
/// <returns>true if the registration is successfully found and removed; otherwise, false.</returns>
public bool Unregister<RegisterType>()
{
return Unregister(typeof(RegisterType), string.Empty);
}

/// <summary>
/// Remove a named container class registration.
/// </summary>
/// <typeparam name="RegisterType">Type to unregister</typeparam>
/// <param name="name">Name of registration</param>
/// <returns>true if the registration is successfully found and removed; otherwise, false.</returns>
public bool Unregister<RegisterType>(string name)
{
return Unregister(typeof(RegisterType), name);
}

/// <summary>
/// Remove a container class registration.
/// </summary>
/// <param name="registerType">Type to unregister</param>
/// <returns>true if the registration is successfully found and removed; otherwise, false.</returns>
public bool Unregister(Type registerType)
{
return Unregister(registerType, string.Empty);
}

/// <summary>
/// Remove a named container class registration.
/// </summary>
/// <param name="registerType">Type to unregister</param>
/// <param name="name">Name of registration</param>
/// <returns>true if the registration is successfully found and removed; otherwise, false.</returns>
public bool Unregister(Type registerType, string name)
{
var typeRegistration = new TypeRegistration(registerType, name);

return RemoveRegistration(typeRegistration);
}

#endregion

#region Resolution
Expand Down Expand Up @@ -3319,9 +3367,9 @@ private RegisterOptions AddUpdateRegistration(TypeRegistration typeRegistration,
return new RegisterOptions(this, typeRegistration);
}

private void RemoveRegistration(TypeRegistration typeRegistration)
private bool RemoveRegistration(TypeRegistration typeRegistration)
{
_RegisteredTypes.Remove(typeRegistration);
return _RegisteredTypes.Remove(typeRegistration);
}

private ObjectFactoryBase GetDefaultObjectFactory(Type registerType, Type registerImplementation)
Expand Down

0 comments on commit 41ca742

Please sign in to comment.