Class Entity<TId>
- Namespace
- Compendium.Core.Domain.Primitives
- Assembly
- Compendium.Core.dll
Base class for all entities in the domain. Provides identity, timestamps, and business rule validation.
public abstract class Entity<TId> : IEquatable<Entity<TId>> where TId : notnull
Type Parameters
TIdThe type of the entity identifier.
- Inheritance
-
Entity<TId>
- Implements
-
IEquatable<Entity<TId>>
- Derived
- Inherited Members
Constructors
Entity(TId)
Initializes a new instance of the Entity<TId> class.
protected Entity(TId id)
Parameters
idTIdThe entity identifier.
Properties
BrokenRules
Gets the collection of broken business rules.
public IReadOnlyCollection<IBusinessRule> BrokenRules { get; }
Property Value
CreatedAt
Gets the timestamp when the entity was created.
public DateTimeOffset CreatedAt { get; }
Property Value
HasBrokenRules
Gets a value indicating whether the entity has any broken business rules.
public bool HasBrokenRules { get; }
Property Value
Id
Gets the entity identifier.
public TId Id { get; }
Property Value
- TId
IsTransient
Gets a value indicating whether this entity is transient (not yet persisted).
public virtual bool IsTransient { get; }
Property Value
ModifiedAt
Gets the timestamp when the entity was last modified.
public DateTimeOffset ModifiedAt { get; }
Property Value
Methods
CheckRule(IBusinessRule)
Checks a business rule and adds it to the broken rules collection if it's broken.
protected void CheckRule(IBusinessRule rule)
Parameters
ruleIBusinessRuleThe business rule to check.
Exceptions
- BusinessRuleValidationException
Thrown when the rule is broken.
ClearBrokenRules()
Clears all broken business rules.
protected void ClearBrokenRules()
Equals(Entity<TId>?)
Determines whether the specified entity is equal to the current entity.
public bool Equals(Entity<TId>? other)
Parameters
otherEntity<TId>The entity to compare with the current entity.
Returns
- bool
true if the specified entity is equal to the current entity; otherwise, false.
Equals(object?)
Determines whether the specified object is equal to the current entity.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current entity.
Returns
- bool
true if the specified object is equal to the current entity; otherwise, false.
GetHashCode()
Returns the hash code for this entity.
public override int GetHashCode()
Returns
- int
A hash code for the current entity.
Touch()
Updates the modified timestamp.
protected void Touch()
Operators
operator ==(Entity<TId>?, Entity<TId>?)
Determines whether two entities are equal.
public static bool operator ==(Entity<TId>? left, Entity<TId>? right)
Parameters
Returns
- bool
true if the entities are equal; otherwise, false.
operator !=(Entity<TId>?, Entity<TId>?)
Determines whether two entities are not equal.
public static bool operator !=(Entity<TId>? left, Entity<TId>? right)
Parameters
Returns
- bool
true if the entities are not equal; otherwise, false.