2026-03-26 10:49:41 +08:00
|
|
|
namespace AlicizaX
|
|
|
|
|
{
|
2026-04-20 13:46:44 +08:00
|
|
|
internal interface IServiceLifecycle
|
2026-03-26 10:49:41 +08:00
|
|
|
{
|
2026-04-20 13:46:44 +08:00
|
|
|
void Initialize(ServiceContext context);
|
|
|
|
|
void Destroy();
|
|
|
|
|
}
|
2026-03-26 10:49:41 +08:00
|
|
|
|
2026-04-20 13:46:44 +08:00
|
|
|
public abstract class ServiceBase : IService, IServiceLifecycle
|
|
|
|
|
{
|
|
|
|
|
protected ServiceContext Context { get; private set; }
|
2026-03-26 10:49:41 +08:00
|
|
|
|
2026-04-20 13:46:44 +08:00
|
|
|
protected bool IsInitialized { get; private set; }
|
2026-03-26 10:49:41 +08:00
|
|
|
|
2026-04-20 13:46:44 +08:00
|
|
|
void IServiceLifecycle.Initialize(ServiceContext context)
|
2026-03-26 10:49:41 +08:00
|
|
|
{
|
|
|
|
|
if (IsInitialized)
|
|
|
|
|
throw new System.InvalidOperationException($"{GetType().FullName} is already initialized.");
|
|
|
|
|
|
|
|
|
|
Context = context;
|
|
|
|
|
IsInitialized = true;
|
|
|
|
|
OnInitialize();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 13:46:44 +08:00
|
|
|
void IServiceLifecycle.Destroy()
|
2026-03-26 10:49:41 +08:00
|
|
|
{
|
|
|
|
|
if (!IsInitialized) return;
|
|
|
|
|
|
|
|
|
|
OnDestroyService();
|
|
|
|
|
IsInitialized = false;
|
|
|
|
|
Context = default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void OnInitialize();
|
|
|
|
|
|
|
|
|
|
protected abstract void OnDestroyService();
|
|
|
|
|
}
|
|
|
|
|
}
|