Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where Singletons are duplicated if the child has the same interface #91

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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