CSharpEssentials.Entity
DDD base classes for aggregate roots with audit fields, soft deletion and domain events.
dotnet add package CSharpEssentials.Entity
using CSharpEssentials.Entity; // EntityBase, SoftDeletableEntityBase
using CSharpEssentials.Entity.Interfaces; // IDomainEvent, ISoftDeletable, IEntityBase
EntityBase\<TId\>
public record OrderCreatedEvent(Guid OrderId, string CustomerId) : IDomainEvent;
public class Order : EntityBase<Guid>
{
public string CustomerId { get; private set; } = default!;
public decimal Total { get; private set; }
public static Order Create(string customerId, decimal total)
{
var order = new Order { Id = Guid.NewGuid(), CustomerId = customerId, Total = total };
order.Raise(new OrderCreatedEvent(order.Id, customerId));
return order;
}
}
// Provided members:
// TId? Id
// DateTimeOffset CreatedAt: set by AuditInterceptor (or SetCreatedInfo); MinValue throws
// string? CreatedBy: set by AuditInterceptor (or SetCreatedInfo)
// DateTimeOffset? UpdatedAt: NOT ModifiedAt
// string? UpdatedBy: NOT ModifiedBy
// IReadOnlyList<IDomainEvent> DomainEvents: property, a cached live read-only view (not a copy)
// void Raise(IDomainEvent)
// void ClearDomainEvents()
SoftDeletableEntityBase\<TId\>
public class Product : SoftDeletableEntityBase<int>
{
public string Name { get; private set; } = default!;
}
var product = new Product();
// Soft delete lifecycle
product.MarkAsDeleted(DateTimeOffset.UtcNow, "admin"); // two params: (deletedAt, deletedBy)
product.MarkAsHardDeleted(); // sets IsHardDeleted
product.Restore(); // clears IsDeleted, DeletedAt, DeletedBy and IsHardDeleted
// Hard-delete a batch: products.HardDelete() calls MarkAsHardDeleted() on each item
// IsHardDeleted makes the EF Core AuditInterceptor remove the row instead of turning the delete into a soft delete
// Additional members:
// DateTimeOffset? DeletedAt
// string? DeletedBy
// bool IsDeleted
// bool IsHardDeleted
Domain events
// Default timing is AfterSave; a BeforeSave event is published before the save and a throwing handler aborts it
[DomainEventTiming(DomainEventTiming.BeforeSave)]
public record InventoryReservedEvent(Guid ProductId, int Qty) : IDomainEvent;
// Raise inside the aggregate (see Order.Create above)
Order order = Order.Create("customer-1", 99m);
// Copy, then clear: DomainEvents is a live view, so an uncopied reference is empty after the clear
IDomainEvent[] events = [.. order.DomainEvents];
order.ClearDomainEvents();
Guidance
- Call
Raise()only inside entity methods to keep domain events encapsulated in the aggregate. DomainEventsis a property; there is noGetDomainEvents(). Raising or clearing while iterating it throwsInvalidOperationException.- Audit fields are
UpdatedAt/UpdatedBy, notModifiedAt/ModifiedBy. MarkAsDeleted()takes two parameters:(DateTimeOffset deletedAt, string deletedBy).- Use
DomainEventTiming.BeforeSavefor events that must be processed before the save; the attribute only labels the event,CSharpEssentials.EntityFrameworkCorepublishes it (BaseDbContextdispatchesAfterSaveevents after the save,DomainEventInterceptorinsideSavingChanges, before the write).