feat: IEntity.Id & BaseEntity
This commit is contained in:
105
Engine.Core/Abstract/BaseEntity.cs
Normal file
105
Engine.Core/Abstract/BaseEntity.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
public abstract class BaseEntity : IEntity
|
||||
{
|
||||
public Action<IEntity, string>? OnIdChanged { get; set; } = null;
|
||||
|
||||
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
|
||||
|
||||
public Action<IInitialize>? OnInitialized { get; set; } = null;
|
||||
public Action<IInitialize>? OnFinalized { get; set; } = null;
|
||||
|
||||
|
||||
private IStateEnable _stateEnable = null!;
|
||||
|
||||
private bool _initialized = false;
|
||||
private string _id = string.Empty;
|
||||
|
||||
public virtual IStateEnable StateEnable => _stateEnable;
|
||||
|
||||
public virtual bool IsActive => StateEnable.Enabled;
|
||||
|
||||
public string Id
|
||||
{
|
||||
get => _id;
|
||||
set
|
||||
{
|
||||
if (value == _id)
|
||||
return;
|
||||
|
||||
string previousId = _id;
|
||||
|
||||
_id = value;
|
||||
OnIdChanged?.Invoke(this, previousId);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Initialized
|
||||
{
|
||||
get => _initialized;
|
||||
private set
|
||||
{
|
||||
if (value == _initialized)
|
||||
return;
|
||||
|
||||
_initialized = value;
|
||||
if (value)
|
||||
OnInitialized?.Invoke(this);
|
||||
else
|
||||
OnFinalized?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Assign(IStateEnable stateEnable)
|
||||
{
|
||||
if (Initialized)
|
||||
return false;
|
||||
|
||||
_stateEnable = stateEnable;
|
||||
_stateEnable.Assign(this);
|
||||
OnStateEnableAssigned?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void UnassignInternal() { }
|
||||
public bool Unassign()
|
||||
{
|
||||
if (Initialized)
|
||||
return false;
|
||||
|
||||
UnassignInternal();
|
||||
|
||||
OnUnassigned?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void InitializeInternal() { }
|
||||
public bool Initialize()
|
||||
{
|
||||
if (Initialized)
|
||||
return false;
|
||||
|
||||
InitializeInternal();
|
||||
|
||||
Initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void FinalizeInternal() { }
|
||||
public bool Finalize()
|
||||
{
|
||||
if (!Initialized)
|
||||
return false;
|
||||
|
||||
FinalizeInternal();
|
||||
|
||||
Initialized = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected BaseEntity() => _id = Guid.NewGuid().ToString("D");
|
||||
protected BaseEntity(string id) => _id = id;
|
||||
}
|
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
/// <summary>
|
||||
@@ -5,4 +7,14 @@ namespace Syntriax.Engine.Core.Abstract;
|
||||
/// </summary>
|
||||
public interface IEntity : IInitialize, IAssignableStateEnable
|
||||
{
|
||||
/// <summary>
|
||||
/// Event triggered when the <see cref="Id"/> of the <see cref="IEntity"/> changes.
|
||||
/// The string action parameter is the previous <see cref="Id"/> of the <see cref="IEntity"/>.
|
||||
/// </summary>
|
||||
Action<IEntity, string>? OnIdChanged { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The ID of the <see cref="IEntity"/>.
|
||||
/// </summary>
|
||||
string Id { get; set; }
|
||||
}
|
||||
|
Reference in New Issue
Block a user