Skip to content

Commit

Permalink
Merge pull request #712 from Geminior/allow-scope-naming-on-create
Browse files Browse the repository at this point in the history
Allow scope naming on create
  • Loading branch information
hadashiA authored Nov 3, 2024
2 parents 931849c + e1850af commit e1e3bdd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
55 changes: 55 additions & 0 deletions VContainer/Assets/Tests/Unity/LifetimeScopeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Linq;
using UnityEngine;
using NUnit.Framework;
using UnityEngine.TestTools;
Expand Down Expand Up @@ -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<SampleEntryPoint>().AsSelf(); },
names[0]),

LifetimeScope.Create(
new DummyInstaller(),
names[1])
};

var childScopes = new[]
{
parentScopes[0].CreateChild(builder => { builder.RegisterEntryPoint<SampleEntryPoint>().AsSelf(); },
names[2]),

parentScopes[0].CreateChild(
new DummyInstaller(),
names[3]),

parentScopes[0].CreateChild<LifetimeScope>(builder => { builder.RegisterEntryPoint<SampleEntryPoint>().AsSelf(); },
names[4]),

parentScopes[0].CreateChild<LifetimeScope>(
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()
// {
Expand Down
24 changes: 12 additions & 12 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ void IDisposable.Dispose()
static readonly Stack<IInstaller> GlobalExtraInstallers = new Stack<IInstaller>();
static readonly object SyncRoot = new object();

static LifetimeScope Create(IInstaller installer = null)
public 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<LifetimeScope>();
if (installer != null)
Expand All @@ -76,8 +76,8 @@ static LifetimeScope Create(IInstaller installer = null)
return newScope;
}

public static LifetimeScope Create(Action<IContainerBuilder> configuration)
=> Create(new ActionInstaller(configuration));
public static LifetimeScope Create(Action<IContainerBuilder> configuration, string name = null)
=> Create(new ActionInstaller(configuration), name);

public static ParentOverrideScope EnqueueParent(LifetimeScope parent)
=> new ParentOverrideScope(parent);
Expand Down Expand Up @@ -226,10 +226,10 @@ void SetContainer(IObjectResolver container)
AutoInjectAll();
}

public TScope CreateChild<TScope>(IInstaller installer = null)
public TScope CreateChild<TScope>(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)
{
Expand All @@ -249,15 +249,15 @@ public TScope CreateChild<TScope>(IInstaller installer = null)
return child;
}

public LifetimeScope CreateChild(IInstaller installer = null)
=> CreateChild<LifetimeScope>(installer);
public LifetimeScope CreateChild(IInstaller installer = null, string childScopeName = null)
=> CreateChild<LifetimeScope>(installer, childScopeName);

public TScope CreateChild<TScope>(Action<IContainerBuilder> installation)
public TScope CreateChild<TScope>(Action<IContainerBuilder> installation, string childScopeName = null)
where TScope : LifetimeScope
=> CreateChild<TScope>(new ActionInstaller(installation));
=> CreateChild<TScope>(new ActionInstaller(installation), childScopeName);

public LifetimeScope CreateChild(Action<IContainerBuilder> installation)
=> CreateChild<LifetimeScope>(new ActionInstaller(installation));
public LifetimeScope CreateChild(Action<IContainerBuilder> installation, string childScopeName = null)
=> CreateChild<LifetimeScope>(new ActionInstaller(installation), childScopeName);

public TScope CreateChildFromPrefab<TScope>(TScope prefab, IInstaller installer = null)
where TScope : LifetimeScope
Expand Down

0 comments on commit e1e3bdd

Please sign in to comment.