Skip to content

Commit

Permalink
Fix unbinding of singleton types. [Fix #50]
Browse files Browse the repository at this point in the history
  • Loading branch information
intentor committed Jul 20, 2016
1 parent 5ddd304 commit 2770c7c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/Assets/Adic/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Copyright (c) 2014-2016 André "Intentor" Martins
http://intentor.com.br/
------------------------------------------------

Version 2.20 (2016-??-??)

Framework
- Fix unbinding of singleton types. [Issue #50]

Version 2.19.1 (2016-07-18)

Event Caller extension
Expand Down
19 changes: 15 additions & 4 deletions src/Assets/Adic/Scripts/Framework/Binding/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,27 @@ public void Unbind<T>() {
public void Unbind(Type type) {
if (!this.ContainsBindingFor(type)) return;

var bindings = this.GetBindingsFor(type);

IList<BindingInfo> bindings = new List<BindingInfo>();
IList<Type> keys = new List<Type>();

foreach (var entry in this.typeBindings) {
for (var bindingIndex = 0; bindingIndex < entry.Value.Count; bindingIndex++) {
var binding = entry.Value[bindingIndex];
if (binding.type.Equals(type) || binding.GetValueType().Equals(type)) {
bindings.Add(binding);
keys.Add(entry.Key);
}
}
}

if (this.beforeRemoveBinding != null) {
this.beforeRemoveBinding(this, type, bindings);
}

this.typeBindings.Remove(type);

foreach (var key in keys) {
this.typeBindings.Remove(key);
}

if (this.afterRemoveBinding != null) {
this.afterRemoveBinding(this, type, bindings);
}
Expand Down
28 changes: 27 additions & 1 deletion src/Assets/Tests/Editor/BinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void TestUnbindByType() {
binder.Unbind(typeof(IMockInterface));

var bindings = binder.GetBindings();

Assert.AreEqual(1, bindings.Count);
Assert.AreEqual(typeof(MockClassToDepend), bindings[0].type);
}
Expand All @@ -154,5 +154,31 @@ public void TestUnbindByIdentifier() {
Assert.AreEqual(typeof(IMockInterface), bindings[1].type);
Assert.AreEqual("Mock2", bindings[1].identifier);
}

[Test]
public void TestUnbindSingleton() {
var binder = new Binder();

binder.Bind<MockIClassWithAttributes>().ToSingleton();

binder.Unbind<MockIClassWithAttributes>();

var bindings = binder.GetBindings();

Assert.AreEqual(0, bindings.Count);
}

[Test]
public void TestUnbindSingletonFromInterface() {
var binder = new Binder();

binder.Bind<IMockInterface>().ToSingleton<MockIClassWithAttributes>();

binder.Unbind<MockIClassWithAttributes>();

var bindings = binder.GetBindings();

Assert.AreEqual(0, bindings.Count);
}
}
}

0 comments on commit 2770c7c

Please sign in to comment.