From 0222106c4e419eda72b72856047e37632a0d2c2c Mon Sep 17 00:00:00 2001 From: Antony Male Date: Wed, 10 Sep 2014 13:54:26 +0100 Subject: [PATCH] Fix up a couple of bugs --- Stylet/StyletIoC/DelegatingDictionary.cs | 31 ++++++++++++++++- Stylet/StyletIoC/StyletIoCBuilder.cs | 2 -- .../StyletIoC/StyletIoCChildContainerTests.cs | 33 +++++++++++++++---- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Stylet/StyletIoC/DelegatingDictionary.cs b/Stylet/StyletIoC/DelegatingDictionary.cs index c37be2d..34dac0b 100644 --- a/Stylet/StyletIoC/DelegatingDictionary.cs +++ b/Stylet/StyletIoC/DelegatingDictionary.cs @@ -76,7 +76,36 @@ namespace StyletIoC public TValue AddOrUpdate(TKey key, Func addValueFactory, Func updateValueFactory) { - return this.ourDictionary.AddOrUpdate(key, addValueFactory, updateValueFactory); + // This is true for all of the current applications in StyletIoC, but may not be true for other applications + // If it's in our dictionary, update + // If it's in our parent dictionary, run it through the translator then the updateValueFactory + // If it's in neither, add it to our dictionary + + TValue returnValue; + bool success; + + do + { + TValue outValue; + if (this.ourDictionary.TryGetValue(key, out outValue)) + { + returnValue = updateValueFactory(key, outValue); + success = this.ourDictionary.TryUpdate(key, returnValue, outValue); + } + else if (this.parentDictionary.TryGetValue(key, out outValue)) + { + returnValue = updateValueFactory(key, this.translator(outValue)); + success = this.ourDictionary.TryAdd(key, returnValue); + } + else + { + returnValue = addValueFactory(key); + success = this.ourDictionary.TryAdd(key, returnValue); + } + } + while (!success); + + return returnValue; } public ICollection Values diff --git a/Stylet/StyletIoC/StyletIoCBuilder.cs b/Stylet/StyletIoC/StyletIoCBuilder.cs index 2eb99ce..900b9ac 100644 --- a/Stylet/StyletIoC/StyletIoCBuilder.cs +++ b/Stylet/StyletIoC/StyletIoCBuilder.cs @@ -455,8 +455,6 @@ namespace StyletIoC public IContainer BuildContainer() { var container = this.parent == null ? new StyletIoCContainer() : new StyletIoCContainer(this.parent); - container.AddRegistration(new TypeKey(typeof(IContainer), null), new SingletonRegistration(container, new FactoryCreator(c => container, container))); - //container.AddRegistration(new TypeKey(typeof(StyletIoCContainer), null), new SingletonRegistration(new FactoryCreator(c => container, container))); // For each TypeKey, we remove any weak bindings if there are any strong bindings var groups = this.bindings.GroupBy(x => new { Key = x.Key, Type = x.ServiceType }); diff --git a/StyletUnitTests/StyletIoC/StyletIoCChildContainerTests.cs b/StyletUnitTests/StyletIoC/StyletIoCChildContainerTests.cs index 4be03bd..d260cbb 100644 --- a/StyletUnitTests/StyletIoC/StyletIoCChildContainerTests.cs +++ b/StyletUnitTests/StyletIoC/StyletIoCChildContainerTests.cs @@ -40,6 +40,10 @@ namespace StyletUnitTests.StyletIoC public bool Disposed; public void Dispose() { this.Disposed = true; } } + interface I1 { } + class C11 : I1 { } + class C12 : I1 { } + class C13 : I1 { } [Test] public void ChildContainerCanAccessRegistrationsOnParent() @@ -130,19 +134,20 @@ namespace StyletUnitTests.StyletIoC } [Test] - public void RecreatingSingletonBindingIntroducedNewScope() + public void CreatingSameBindingOnParentAndChildCausesMultipleRegistrations() { var builder = new StyletIoCBuilder(); - builder.Bind().ToSelf().InSingletonScope(); + builder.Bind().To(); var parent = builder.BuildContainer(); var childBuilder = parent.CreateChildBuilder(); - childBuilder.Bind().ToSelf().InSingletonScope(); + childBuilder.Bind().To(); var child = childBuilder.BuildContainer(); - Assert.AreNotEqual(parent.Get(), child.Get()); - Assert.AreEqual(parent.Get(), parent.Get()); - Assert.AreEqual(child.Get(), child.Get()); + var r = child.GetAll(); + + Assert.AreEqual(2, child.GetAll().Count()); + Assert.AreEqual(1, parent.GetAll().Count()); } [Test] @@ -197,6 +202,22 @@ namespace StyletUnitTests.StyletIoC Assert.Throws(() => parent.Get>()); } + [Test] + public void ChildExtendsButDoesNotModifyGetAllRegistrations() + { + var builder = new StyletIoCBuilder(); + builder.Bind().To(); + builder.Bind().To(); + var parent = builder.BuildContainer(); + + var childBuilder = parent.CreateChildBuilder(); + childBuilder.Bind().To(); + var child = childBuilder.BuildContainer(); + + Assert.AreEqual(3, child.GetAll().Count()); + Assert.AreEqual(2, parent.GetAll().Count()); + } + [Test] public void ContainerDisposesItsSingletonsWhenRequested() {