This commit is contained in:
陈思海 2026-03-26 19:50:59 +08:00
parent f5187eac91
commit 910c3b916a
6 changed files with 23 additions and 15 deletions

View File

@ -3,5 +3,13 @@ namespace AlicizaX
/// <summary> /// <summary>
/// 框架内置的 App Scope 标记类,生命周期与 ServiceWorld 相同。 /// 框架内置的 App Scope 标记类,生命周期与 ServiceWorld 相同。
/// </summary> /// </summary>
public sealed class AppScope { } public sealed class AppScope : IScope
{
public int Order => -10000;
}
public interface IScope
{
public int Order { get; }
}
} }

View File

@ -29,19 +29,19 @@ namespace AlicizaX
// ── Scope 管理 ────────────────────────────────────────────────────────── // ── Scope 管理 ──────────────────────────────────────────────────────────
public static ServiceScope CreateScope<TScope>(int order = 0) where TScope : class public static ServiceScope CreateScope<TScope>(int order = 0) where TScope : IScope
=> World.CreateScope<TScope>(order); => World.CreateScope<TScope>(order);
public static ServiceScope GetOrCreateScope<TScope>(int order = 0) where TScope : class public static ServiceScope GetOrCreateScope<TScope>(int order = 0) where TScope : IScope
=> World.GetOrCreateScope<TScope>(order); => World.GetOrCreateScope<TScope>(order);
public static bool TryGetScope<TScope>(out ServiceScope scope) where TScope : class public static bool TryGetScope<TScope>(out ServiceScope scope) where TScope : IScope
{ {
if (_world == null) { scope = null; return false; } if (_world == null) { scope = null; return false; }
return _world.TryGetScope<TScope>(out scope); return _world.TryGetScope<TScope>(out scope);
} }
public static bool DestroyScope<TScope>() where TScope : class public static bool DestroyScope<TScope>() where TScope : IScope
{ {
if (_world == null) return false; if (_world == null) return false;
return _world.DestroyScope<TScope>(); return _world.DestroyScope<TScope>();

View File

@ -20,13 +20,13 @@ namespace AlicizaX
public bool TryGet<T>(out T service) where T : class, IService public bool TryGet<T>(out T service) where T : class, IService
=> World.TryGet(Scope, out service); => World.TryGet(Scope, out service);
public ServiceScope CreateScope<TScope>(int order = 0) where TScope : class public ServiceScope CreateScope<TScope>(int order = 0) where TScope : IScope
=> World.CreateScope<TScope>(order); => World.CreateScope<TScope>(order);
public ServiceScope GetOrCreateScope<TScope>(int order = 0) where TScope : class public ServiceScope GetOrCreateScope<TScope>(int order = 0) where TScope : IScope
=> World.GetOrCreateScope<TScope>(order); => World.GetOrCreateScope<TScope>(order);
public bool TryGetScope<TScope>(out ServiceScope scope) where TScope : class public bool TryGetScope<TScope>(out ServiceScope scope) where TScope : IScope
=> World.TryGetScope<TScope>(out scope); => World.TryGetScope<TScope>(out scope);
} }
} }

View File

@ -20,7 +20,7 @@ namespace AlicizaX
// ── Scope 管理Type-based ──────────────────────────────────────────── // ── Scope 管理Type-based ────────────────────────────────────────────
public ServiceScope CreateScope<TScope>(int order = 0) where TScope : class public ServiceScope CreateScope<TScope>(int order = 0) where TScope : IScope
{ {
var type = typeof(TScope); var type = typeof(TScope);
if (_scopesByType.ContainsKey(type)) if (_scopesByType.ContainsKey(type))
@ -28,7 +28,7 @@ namespace AlicizaX
return CreateScopeInternal(type, order); return CreateScopeInternal(type, order);
} }
public ServiceScope GetOrCreateScope<TScope>(int order = 0) where TScope : class public ServiceScope GetOrCreateScope<TScope>(int order = 0) where TScope : IScope
{ {
var type = typeof(TScope); var type = typeof(TScope);
if (_scopesByType.TryGetValue(type, out var existing)) if (_scopesByType.TryGetValue(type, out var existing))
@ -36,16 +36,16 @@ namespace AlicizaX
return CreateScopeInternal(type, order); return CreateScopeInternal(type, order);
} }
public bool TryGetScope<TScope>(out ServiceScope scope) where TScope : class public bool TryGetScope<TScope>(out ServiceScope scope) where TScope : IScope
=> _scopesByType.TryGetValue(typeof(TScope), out scope); => _scopesByType.TryGetValue(typeof(TScope), out scope);
public ServiceScope GetScope<TScope>() where TScope : class public ServiceScope GetScope<TScope>() where TScope : IScope
{ {
if (TryGetScope<TScope>(out var scope)) return scope; if (TryGetScope<TScope>(out var scope)) return scope;
throw new InvalidOperationException($"Scope {typeof(TScope).Name} does not exist."); throw new InvalidOperationException($"Scope {typeof(TScope).Name} does not exist.");
} }
public bool DestroyScope<TScope>() where TScope : class public bool DestroyScope<TScope>() where TScope : IScope
{ {
if (typeof(TScope) == typeof(AppScope)) if (typeof(TScope) == typeof(AppScope))
throw new InvalidOperationException("AppScope can only be destroyed when the world is disposed."); throw new InvalidOperationException("AppScope can only be destroyed when the world is disposed.");

View File

@ -53,7 +53,7 @@ namespace AlicizaX
/// </para> /// </para>
/// </summary> /// </summary>
public abstract class MonoServiceBehaviour<TScope> : MonoServiceBehaviour public abstract class MonoServiceBehaviour<TScope> : MonoServiceBehaviour
where TScope : class where TScope : IScope
{ {
[SerializeField] private bool _dontDestroyOnLoad = false; [SerializeField] private bool _dontDestroyOnLoad = false;

View File

@ -7,7 +7,7 @@ using UnityEngine;
namespace AlicizaX namespace AlicizaX
{ {
public sealed class GameObjectPoolManager : MonoServiceBehaviour<GameObjectPoolManager> public sealed class GameObjectPoolManager : MonoServiceBehaviour<AppScope>
{ {
private static readonly Comparison<GameObjectPoolSnapshot> SnapshotComparer = (left, right) => private static readonly Comparison<GameObjectPoolSnapshot> SnapshotComparer = (left, right) =>
{ {