From 6cb626c20bdd39b574f0f94d40bbbc05d9470cb2 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 29 Nov 2024 18:33:32 +0900 Subject: [PATCH] Fix bug where DisposeCallback would not close to parent and child --- VContainer/Assets/Tests/ContainerTest.cs | 25 +++++++++++++++++++ .../Runtime/ContainerBuilderExtensions.cs | 4 +-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/VContainer/Assets/Tests/ContainerTest.cs b/VContainer/Assets/Tests/ContainerTest.cs index 635d6618..6e5e86b1 100644 --- a/VContainer/Assets/Tests/ContainerTest.cs +++ b/VContainer/Assets/Tests/ContainerTest.cs @@ -617,6 +617,31 @@ public void OnContainerDisposeCallback() Assert.That(resolvedJustBeforeDispose2, Is.Not.Null); } + [Test] + public void OnContainerDisposeCallback_ParentChild() + { + var parentDisposeCalled = false; + var childDisposeCalled = false; + + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Scoped); + + builder.RegisterDisposeCallback(x => parentDisposeCalled = true); + + var container = builder.Build(); + + var childContainer = container.CreateScope(childBuilder => + { + childBuilder.Register(Lifetime.Scoped); + childBuilder.RegisterDisposeCallback(x => childDisposeCalled = true); + }); + + childContainer.Dispose(); + + Assert.That(childDisposeCalled, Is.True); + Assert.That(parentDisposeCalled, Is.False); + } + [Test] public void TryResolveTransient() { diff --git a/VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs b/VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs index e82d7ade..fd8f71bb 100644 --- a/VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs +++ b/VContainer/Assets/VContainer/Runtime/ContainerBuilderExtensions.cs @@ -143,8 +143,8 @@ public static RegistrationBuilder RegisterFactory callback) { - builder.Register(container => new BuilderCallbackDisposable(callback, container), Lifetime.Scoped); - builder.RegisterBuildCallback(container => container.Resolve>()); + builder.Register(container => new BuilderCallbackDisposable(callback, container), Lifetime.Singleton); + builder.RegisterBuildCallback(container => container.Resolve>>()); } [Obsolete("IObjectResolver is registered by default. This method does nothing.")]