From 5feadaeb6bd4444db9ea34e4e85bf47e647f651d Mon Sep 17 00:00:00 2001 From: Thomas Scheelhardt Date: Thu, 10 Oct 2024 08:57:41 +0200 Subject: [PATCH 1/4] Allow naming scopes and child scopes on create --- .../VContainer/Runtime/Unity/LifetimeScope.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index 08bf8a42..6125b99c 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -63,9 +63,9 @@ void IDisposable.Dispose() static readonly Stack GlobalExtraInstallers = new Stack(); static readonly object SyncRoot = new object(); - static LifetimeScope Create(IInstaller installer = null) + static LifetimeScope Create(IInstaller installer = null, string name = null) { - var gameObject = new GameObject("LifetimeScope"); + var gameObject = new GameObject(name ?? "LifetimeScope"); gameObject.SetActive(false); var newScope = gameObject.AddComponent(); if (installer != null) @@ -226,10 +226,10 @@ void SetContainer(IObjectResolver container) AutoInjectAll(); } - public TScope CreateChild(IInstaller installer = null) + public TScope CreateChild(IInstaller installer = null, string childScopeName = null) where TScope : LifetimeScope { - var childGameObject = new GameObject("LifetimeScope (Child)"); + var childGameObject = new GameObject(childScopeName ?? "LifetimeScope (Child)"); childGameObject.SetActive(false); if (IsRoot) { @@ -249,15 +249,15 @@ public TScope CreateChild(IInstaller installer = null) return child; } - public LifetimeScope CreateChild(IInstaller installer = null) - => CreateChild(installer); + public LifetimeScope CreateChild(IInstaller installer = null, string childScopeName = null) + => CreateChild(installer, childScopeName); - public TScope CreateChild(Action installation) + public TScope CreateChild(Action installation, string childScopeName = null) where TScope : LifetimeScope - => CreateChild(new ActionInstaller(installation)); + => CreateChild(new ActionInstaller(installation), childScopeName); - public LifetimeScope CreateChild(Action installation) - => CreateChild(new ActionInstaller(installation)); + public LifetimeScope CreateChild(Action installation, string childScopeName = null) + => CreateChild(new ActionInstaller(installation), childScopeName); public TScope CreateChildFromPrefab(TScope prefab, IInstaller installer = null) where TScope : LifetimeScope From 31eb7b92e326d535d64e4ff126ee26ba4ce68f4b Mon Sep 17 00:00:00 2001 From: Thomas Scheelhardt Date: Thu, 10 Oct 2024 09:35:46 +0200 Subject: [PATCH 2/4] add missing overload --- VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index 6125b99c..19a254cb 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -76,8 +76,8 @@ static LifetimeScope Create(IInstaller installer = null, string name = null) return newScope; } - public static LifetimeScope Create(Action configuration) - => Create(new ActionInstaller(configuration)); + public static LifetimeScope Create(Action configuration, string name = null) + => Create(new ActionInstaller(configuration), name); public static ParentOverrideScope EnqueueParent(LifetimeScope parent) => new ParentOverrideScope(parent); From 22752570632527ff2127b57133f83a8130b3d174 Mon Sep 17 00:00:00 2001 From: Thomas Scheelhardt Date: Thu, 10 Oct 2024 09:36:19 +0200 Subject: [PATCH 3/4] Update LifetimeScope.cs Make Create(IInstaller) public --- VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs index 19a254cb..c4fb8bd5 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs @@ -63,7 +63,7 @@ void IDisposable.Dispose() static readonly Stack GlobalExtraInstallers = new Stack(); static readonly object SyncRoot = new object(); - static LifetimeScope Create(IInstaller installer = null, string name = null) + public static LifetimeScope Create(IInstaller installer = null, string name = null) { var gameObject = new GameObject(name ?? "LifetimeScope"); gameObject.SetActive(false); From e1850afd984697bbee1a2e2afe66bf32640fcce9 Mon Sep 17 00:00:00 2001 From: Thomas Scheelhardt Date: Thu, 10 Oct 2024 09:37:17 +0200 Subject: [PATCH 4/4] test for creating scopes with name --- .../Assets/Tests/Unity/LifetimeScopeTest.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/VContainer/Assets/Tests/Unity/LifetimeScopeTest.cs b/VContainer/Assets/Tests/Unity/LifetimeScopeTest.cs index 23fd230f..9499513c 100644 --- a/VContainer/Assets/Tests/Unity/LifetimeScopeTest.cs +++ b/VContainer/Assets/Tests/Unity/LifetimeScopeTest.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Linq; using UnityEngine; using NUnit.Framework; using UnityEngine.TestTools; @@ -225,6 +226,60 @@ public void ParentTypeReference() Assert.That(child.Parent, Is.EqualTo(parent)); } + [Test] + public void CreateWithName() + { + var names = Enumerable.Range(0, 6).Select(_ => GenerateRandomString()).ToArray(); + + var parentScopes = new[] + { + LifetimeScope.Create(builder => { builder.RegisterEntryPoint().AsSelf(); }, + names[0]), + + LifetimeScope.Create( + new DummyInstaller(), + names[1]) + }; + + var childScopes = new[] + { + parentScopes[0].CreateChild(builder => { builder.RegisterEntryPoint().AsSelf(); }, + names[2]), + + parentScopes[0].CreateChild( + new DummyInstaller(), + names[3]), + + parentScopes[0].CreateChild(builder => { builder.RegisterEntryPoint().AsSelf(); }, + names[4]), + + parentScopes[0].CreateChild( + new DummyInstaller(), + names[5]) + }; + + for (var i = 0; i < parentScopes.Length; i++) + { + Assert.AreEqual(names[i], parentScopes[i].name); + } + + for (var i = 0; i < childScopes.Length; i++) + { + Assert.AreEqual(names[i + parentScopes.Length], childScopes[i].name); + } + } + + private class DummyInstaller : IInstaller + { + public void Install(IContainerBuilder builder) + { + /* NOOP */ + } + } + + private static string GenerateRandomString(int size = 10) => + string.Join(string.Empty, Enumerable.Range(0, size).Select(_ => (char)Random.Range(97, 123))); + // [Test] // public void ParentTypeReferenceInvalid() // {