Fix up a couple of bugs

This commit is contained in:
Antony Male 2014-09-10 13:54:26 +01:00
parent 37289a2b83
commit 0222106c4e
3 changed files with 57 additions and 9 deletions

View File

@ -76,7 +76,36 @@ namespace StyletIoC
public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> 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<TValue> Values

View File

@ -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<StyletIoCContainer>(c => container, container)));
//container.AddRegistration(new TypeKey(typeof(StyletIoCContainer), null), new SingletonRegistration(new FactoryCreator<StyletIoCContainer>(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 });

View File

@ -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<C1>().ToSelf().InSingletonScope();
builder.Bind<I1>().To<C11>();
var parent = builder.BuildContainer();
var childBuilder = parent.CreateChildBuilder();
childBuilder.Bind<C1>().ToSelf().InSingletonScope();
childBuilder.Bind<I1>().To<C12>();
var child = childBuilder.BuildContainer();
Assert.AreNotEqual(parent.Get<C1>(), child.Get<C1>());
Assert.AreEqual(parent.Get<C1>(), parent.Get<C1>());
Assert.AreEqual(child.Get<C1>(), child.Get<C1>());
var r = child.GetAll<I1>();
Assert.AreEqual(2, child.GetAll<I1>().Count());
Assert.AreEqual(1, parent.GetAll<I1>().Count());
}
[Test]
@ -197,6 +202,22 @@ namespace StyletUnitTests.StyletIoC
Assert.Throws<StyletIoCRegistrationException>(() => parent.Get<IValidator<int>>());
}
[Test]
public void ChildExtendsButDoesNotModifyGetAllRegistrations()
{
var builder = new StyletIoCBuilder();
builder.Bind<I1>().To<C11>();
builder.Bind<I1>().To<C12>();
var parent = builder.BuildContainer();
var childBuilder = parent.CreateChildBuilder();
childBuilder.Bind<I1>().To<C13>();
var child = childBuilder.BuildContainer();
Assert.AreEqual(3, child.GetAll<I1>().Count());
Assert.AreEqual(2, parent.GetAll<I1>().Count());
}
[Test]
public void ContainerDisposesItsSingletonsWhenRequested()
{