Skip to content

Commit

Permalink
Merge pull request #91 from hadashiA/ku/fix-duplicate-singleton
Browse files Browse the repository at this point in the history
Fix a bug where Singletons are duplicated if the child has the same interface
  • Loading branch information
hadashiA authored Jan 15, 2021
2 parents 9e75a81 + 18077da commit bab0877
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion VContainer/Assets/VContainer/Runtime/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public object Resolve(IRegistration registration)
if (Parent is null)
return Root.Resolve(registration);

if (!registry.Exists(registration))
if (!registry.Exists(registration.ImplementationType))
return Parent.Resolve(registration);

return CreateTrackedInstance(registration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,6 @@ public bool TryGet(Type interfaceType, out IRegistration registration)
return false;
}

public bool Exists(IRegistration registration)
{
if (registration.InterfaceTypes?.Count > 0)
{
foreach (var type in registration.InterfaceTypes)
{
if (hashTable.TryGet(type, out _))
{
return true;
}
}
return false;
}
return hashTable.TryGet(registration.ImplementationType, out _);
}
public bool Exists(Type type) => hashTable.TryGet(type, out _);
}
}
}
2 changes: 1 addition & 1 deletion VContainer/Assets/VContainer/Runtime/Internal/IRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ interface IRegistry
{
// void Add(Registration registration);
bool TryGet(Type interfaceType, out IRegistration registration);
bool Exists(IRegistration registration);
bool Exists(Type implementationType);
}
}
18 changes: 14 additions & 4 deletions VContainer/Assets/VContainer/Tests/ScopedContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,23 @@ public void CreateScopeAndRegister()
public void CreateScopeWithResolveSingleton()
{
var builder = new ContainerBuilder();
builder.Register<NoDependencyServiceA>(Lifetime.Singleton);
builder.Register<DisposableServiceA>(Lifetime.Singleton)
.AsImplementedInterfaces()
.AsSelf();

var container = builder.Build();
var scopedContainer = container.CreateScope();
var scopedContainer = container.CreateScope(childBuilder =>
{
childBuilder.Register<DisposableServiceB>(Lifetime.Singleton)
.AsImplementedInterfaces()
.AsSelf();
});

var singleton = scopedContainer.Resolve<NoDependencyServiceA>();
Assert.That(singleton, Is.InstanceOf<NoDependencyServiceA>());
var singleton = container.Resolve<DisposableServiceA>();
var singleton2 = scopedContainer.Resolve<DisposableServiceA>();
Assert.That(singleton, Is.InstanceOf<DisposableServiceA>());
Assert.That(singleton2, Is.InstanceOf<DisposableServiceA>());
Assert.That(singleton, Is.EqualTo(singleton2));
}

[Test]
Expand Down

1 comment on commit bab0877

@vercel
Copy link

@vercel vercel bot commented on bab0877 Jan 15, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.