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

Adding Singleton Lifetime Scope #741

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using VContainer.Unity.Extensions;

namespace VContainer.Tests.Unity
{
public sealed class SampleNestedSingletonLifetimeScope : NestedSingletonLifetimeScope<SampleSingletonLifetimeScope>
{
protected override void Configure(IContainerBuilder builder)
{
builder.Register<DisposableServiceA>(Lifetime.Scoped);
builder.Register<DisposableServiceB>(Lifetime.Singleton);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using VContainer.Unity.Extensions;

namespace VContainer.Tests.Unity
{
public sealed class SampleSingletonLifetimeScope : SingletonLifetimeScope<SampleSingletonLifetimeScope>
{
protected override void Configure(IContainerBuilder builder)
{
builder.Register<DisposableServiceA>(Lifetime.Scoped);
builder.Register<DisposableServiceB>(Lifetime.Singleton);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions VContainer/Assets/Tests/Unity/SingletonLifetimeScopeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;

namespace VContainer.Tests.Unity
{
public class SingletonLifetimeScopeTest
{
[UnityTest]
public IEnumerator Create()
{
var parentLifetimeScope = Create<SampleSingletonLifetimeScope>("Parent");

yield return null;
yield return null;

var childLifetimeScope = Create<SampleNestedSingletonLifetimeScope>("Child");

yield return null;
yield return null;

var parentDisposableA = parentLifetimeScope.Container.Resolve<DisposableServiceA>();
var parentDisposableB = parentLifetimeScope.Container.Resolve<DisposableServiceB>();
var childDisposableA = childLifetimeScope.Container.Resolve<DisposableServiceA>();
var childDisposableB = parentLifetimeScope.Container.Resolve<DisposableServiceB>();

Assert.That(parentDisposableB, Is.SameAs(childDisposableB));

// Scene reload
var currentSceneName = SceneManager.GetActiveScene().name;
SceneManager.LoadScene(currentSceneName);

yield return null;
yield return null;

Assert.That(SceneManager.GetActiveScene().name, Is.EqualTo(currentSceneName));
Assert.That(parentLifetimeScope != null, Is.True);
Assert.That(childLifetimeScope == null, Is.True);
Assert.That(parentDisposableA.Disposed, Is.False);
Assert.That(childDisposableA.Disposed, Is.True);
Assert.That(parentDisposableB.Disposed, Is.False);
Assert.That(childDisposableB.Disposed, Is.False);
}

T Create<T>(string name) where T : Component
{
var obj = new GameObject(name);
obj.SetActive(false);
var component = obj.AddComponent<T>();
obj.SetActive(true);
return component;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ static string[] GetAllTypeNames()
{
return new List<string> { "None" }
.Concat(TypeCache.GetTypesDerivedFrom<LifetimeScope>()
.Select(type => type.FullName))
.Where(type => !type.IsAbstract)
.Select(type => type.FullName))
.ToArray();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using UnityEditor;
using VContainer.Unity.Extensions;

namespace VContainer.Editor
{
[CanEditMultipleObjects]
[CustomEditor(typeof(SingletonLifetimeScopeBase), true)]
public sealed class SingletonLifetimeScopeBaseEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
{
EditorGUI.BeginDisabledGroup(true);
var property = serializedObject.FindProperty("m_Script");
EditorGUILayout.PropertyField(property, true);
EditorGUI.EndDisabledGroup();
}
{
var property = serializedObject.GetIterator();
property.NextVisible(true);
while (property.NextVisible(false))
{
var isParentReference = property.propertyType == SerializedPropertyType.Generic
&& property.type == nameof(global::VContainer.Unity.ParentReference);

EditorGUI.BeginDisabledGroup(isParentReference);
EditorGUILayout.PropertyField(property, true);
EditorGUI.EndDisabledGroup();
}
}
serializedObject.ApplyModifiedProperties();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Unity/Extensions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using UnityEngine;

namespace VContainer.Unity.Extensions
{
public abstract class NestedSingletonLifetimeScope<T> : SingletonLifetimeScopeBase where T : SingletonLifetimeScope<T>
{
protected void OnValidate()
{
if (parentReference.Type != typeof(T))
{
Reset();
}
}

protected virtual void Reset()
{
parentReference = ParentReference.Create<T>();
}

protected override LifetimeScope FindParent()
{
var objs = FindObjects(parentReference.Type);
if (objs.Length > 1)
{
foreach (var obj in objs)
{
var scope = (LifetimeScope) obj;
if (scope.gameObject.scene.name == "DontDestroyOnLoad"
&& scope.Container != null)
{
return scope;
}
}
}
{
if (objs.Length > 0
&& objs[0] is LifetimeScope scope
&& scope.Container != null)
{
return scope;
}
}
return null;
}

static UnityEngine.Object[] FindObjects(Type type)
{
#if UNITY_2022_1_OR_NEWER
return FindObjectsByType(type, FindObjectsInactive.Exclude, FindObjectsSortMode.None);
#else
return FindObjectsOfType(type);
#endif
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using UnityEngine;

namespace VContainer.Unity.Extensions
{
public abstract class SingletonLifetimeScope<T> : SingletonLifetimeScopeBase where T : SingletonLifetimeScope<T>
{
static T s_instance;

public static T Instance
{
get
{
if (s_instance == null)
{
s_instance = FindObject();
if (s_instance != null)
{
s_instance.Initialize();
}
else
{
Debug.LogError($"The instance of {typeof(T).Name} was not found. Please make sure it is present in the scene.");
}
}
return s_instance;
}
}

void Initialize()
{
base.Awake();
s_instance = (T) this;
DontDestroyOnLoad(gameObject);
}

protected override void Awake()
{
if (s_instance == null)
{
Initialize();
}
else if (s_instance != this)
{
Destroy(gameObject);
}
}

protected override void OnDestroy()
{
if (s_instance == this)
{
base.OnDestroy();
s_instance = null;
}
}

protected void OnValidate()
{
if (parentReference.Type != null)
{
Reset();
}
}

protected virtual void Reset()
{
parentReference = default;
}

static T FindObject()
{
#if UNITY_2022_1_OR_NEWER
return FindAnyObjectByType<T>();
#else
return FindObjectOfType<T>();
#endif
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace VContainer.Unity.Extensions
{
public abstract class SingletonLifetimeScopeBase : LifetimeScope {}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading