From 18077dadceaea290c3f1172d795304ceb0b57c91 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Sat, 16 Jan 2021 03:45:16 +0900 Subject: [PATCH] Fix a bug where Singletons are duplicated if the child has the same interface --- .../Assets/VContainer/Runtime/Container.cs | 2 +- .../Internal/FixedTypeKeyHashTableRegistry.cs | 18 ++---------------- .../VContainer/Runtime/Internal/IRegistry.cs | 2 +- .../VContainer/Tests/ScopedContainerTest.cs | 18 ++++++++++++++---- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Container.cs b/VContainer/Assets/VContainer/Runtime/Container.cs index 9aac356d..88a1d252 100644 --- a/VContainer/Assets/VContainer/Runtime/Container.cs +++ b/VContainer/Assets/VContainer/Runtime/Container.cs @@ -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); diff --git a/VContainer/Assets/VContainer/Runtime/Internal/FixedTypeKeyHashTableRegistry.cs b/VContainer/Assets/VContainer/Runtime/Internal/FixedTypeKeyHashTableRegistry.cs index 2d819123..e156ce30 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/FixedTypeKeyHashTableRegistry.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/FixedTypeKeyHashTableRegistry.cs @@ -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 _); } -} \ No newline at end of file +} diff --git a/VContainer/Assets/VContainer/Runtime/Internal/IRegistry.cs b/VContainer/Assets/VContainer/Runtime/Internal/IRegistry.cs index 06087b3c..e064a18c 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/IRegistry.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/IRegistry.cs @@ -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); } } \ No newline at end of file diff --git a/VContainer/Assets/VContainer/Tests/ScopedContainerTest.cs b/VContainer/Assets/VContainer/Tests/ScopedContainerTest.cs index 47db398f..1852e6c7 100644 --- a/VContainer/Assets/VContainer/Tests/ScopedContainerTest.cs +++ b/VContainer/Assets/VContainer/Tests/ScopedContainerTest.cs @@ -80,13 +80,23 @@ public void CreateScopeAndRegister() public void CreateScopeWithResolveSingleton() { var builder = new ContainerBuilder(); - builder.Register(Lifetime.Singleton); + builder.Register(Lifetime.Singleton) + .AsImplementedInterfaces() + .AsSelf(); var container = builder.Build(); - var scopedContainer = container.CreateScope(); + var scopedContainer = container.CreateScope(childBuilder => + { + childBuilder.Register(Lifetime.Singleton) + .AsImplementedInterfaces() + .AsSelf(); + }); - var singleton = scopedContainer.Resolve(); - Assert.That(singleton, Is.InstanceOf()); + var singleton = container.Resolve(); + var singleton2 = scopedContainer.Resolve(); + Assert.That(singleton, Is.InstanceOf()); + Assert.That(singleton2, Is.InstanceOf()); + Assert.That(singleton, Is.EqualTo(singleton2)); } [Test]