CSharpEssentials.Entity
8.0.0
dotnet add package CSharpEssentials.Entity --version 8.0.0
NuGet\Install-Package CSharpEssentials.Entity -Version 8.0.0
<PackageReference Include="CSharpEssentials.Entity" Version="8.0.0" />
<PackageVersion Include="CSharpEssentials.Entity" Version="8.0.0" />
<PackageReference Include="CSharpEssentials.Entity" />
paket add CSharpEssentials.Entity --version 8.0.0
#r "nuget: CSharpEssentials.Entity, 8.0.0"
#:package CSharpEssentials.Entity@8.0.0
#addin nuget:?package=CSharpEssentials.Entity&version=8.0.0
#tool nuget:?package=CSharpEssentials.Entity&version=8.0.0
CSharpEssentials.Entity
Base classes for DDD entities with audit fields, soft deletion and domain events.
EntityBase<TId>: a typedId(protected setter) plus creation and modification audit fields.EntityBaseis the same without an ID.SoftDeletableEntityBase<TId>andSoftDeletableEntityBase: addMarkAsDeleted,RestoreandMarkAsHardDeleted.- Domain events:
Raise,DomainEventsandClearDomainEvents, with[DomainEventTiming]to choose when the EF Core package publishes each event. - Interfaces for code that works with any entity:
IEntityBase,IEntityBase<TId>,ISoftDeletable,ICreationAudit,IModificationAudit,IDomainEventHolderandIDomainEvent, in namespaceCSharpEssentials.Entity.Interfaces.
Benchmarks of the EntityBase domain event list against a hand-written list (read, raise and clear, collect) are in BENCHMARKS.md.
Installation
dotnet add package CSharpEssentials.Entity
Targets net11.0, net10.0, net9.0 and netstandard2.1. Depends on CSharpEssentials.Results and CSharpEssentials.Time.
Usage
Basic entity
using CSharpEssentials.Entity;
public class Product : EntityBase<Guid>
{
public Product(Guid id) => Id = id;
public string Name { get; set; } = string.Empty;
}
var product = new Product(Guid.NewGuid()) { Name = "Laptop" };
product.SetCreatedInfo(DateTimeOffset.UtcNow, "system");
product.SetUpdatedInfo(DateTimeOffset.UtcNow, "admin");
Soft-deletable entity
public class User : SoftDeletableEntityBase<Guid>
{
public string Name { get; set; } = string.Empty;
}
var user = new User();
user.MarkAsDeleted(DateTimeOffset.UtcNow, "Admin"); // IsDeleted, DeletedAt, DeletedBy
user.Restore(); // clears IsDeleted, DeletedAt, DeletedBy and IsHardDeleted
MarkAsHardDeleted() sets IsHardDeleted, which tells the EF Core AuditInterceptor to remove the row instead of converting the delete into a soft delete. The HardDelete() extension (on IEnumerable<T> where T : ISoftDeletable) marks every item. The audit properties (CreatedAt, CreatedBy, UpdatedAt, UpdatedBy, DeletedAt, DeletedBy) have private setters; set them with SetCreatedInfo, SetUpdatedInfo and MarkAsDeleted. SetCreatedInfo throws ArgumentOutOfRangeException for DateTimeOffset.MinValue.
Domain events
using CSharpEssentials.Entity;
using CSharpEssentials.Entity.Interfaces;
public record OrderCreated(Guid OrderId) : IDomainEvent;
public class Order : EntityBase<Guid>
{
public void Create()
{
Id = Guid.NewGuid();
Raise(new OrderCreated(Id));
}
}
var order = new Order();
order.Create();
IDomainEvent[] events = [.. order.DomainEvents];
order.ClearDomainEvents();
// dispatch events...
Raise throws ArgumentNullException for null. DomainEvents is a read-only live view of the entity's events, so reading it does not copy the list. Raising or clearing events while you loop over it throws InvalidOperationException; copy it first with [.. order.DomainEvents], as above, when you clear before you dispatch. The EF Core package collects the events of tracked entities and dispatches them; see CSharpEssentials.EntityFrameworkCore.
Event timing
// AfterSave (default): published after the save
public record OrderCreatedEvent(Guid OrderId) : IDomainEvent;
// Published before the save; a handler that throws aborts it
[DomainEventTiming(DomainEventTiming.BeforeSave)]
public record OrderValidationEvent(Guid OrderId) : IDomainEvent;
The attribute only labels the event; CSharpEssentials.EntityFrameworkCore reads it. BaseDbContext with DispatchDomainEventsOnSaveChanges publishes AfterSave events once the save has succeeded. DomainEventInterceptor handles them inside SavingChanges, still before the database write, so that an outbox can store them in the same save.
Upgrading to 8.0
EntityBase.DomainEventsreturns a read-only live view instead of a new copy on every read. 7.x copied the list each time, so a reference kept beforeClearDomainEvents()still held the old events, and raising or clearing inside aforeachover it was safe. Now a kept reference shows laterRaiseandClearDomainEventscalls, and changing the events during aforeachthrowsInvalidOperationException. If you read the events and then clear them, copy them first:IDomainEvent[] events = [.. entity.DomainEvents];.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- CSharpEssentials.Results (>= 8.0.0)
- CSharpEssentials.Time (>= 8.0.0)
- Microsoft.Bcl.TimeProvider (>= 8.0.0)
- System.Text.Json (>= 9.0.4)
-
net10.0
- CSharpEssentials.Results (>= 8.0.0)
- CSharpEssentials.Time (>= 8.0.0)
-
net11.0
- CSharpEssentials.Results (>= 8.0.0)
- CSharpEssentials.Time (>= 8.0.0)
-
net9.0
- CSharpEssentials.Results (>= 8.0.0)
- CSharpEssentials.Time (>= 8.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on CSharpEssentials.Entity:
| Package | Downloads |
|---|---|
|
CSharpEssentials
A comprehensive C# library enhancing functional programming capabilities with type-safe monads (Maybe, Result), discriminated unions (Any), and robust error handling. Features include: domain-driven design support, enhanced Entity Framework integration, testable time management, JSON utilities, and LINQ extensions. Built for modern C# development with focus on maintainability, testability, and functional programming principles. |
|
|
CSharpEssentials.EntityFrameworkCore
Enhances Entity Framework Core with functional programming patterns and DDD-friendly features. Includes base entity classes, soft delete support, audit trails, query filters, optimistic concurrency, PostgreSQL integration, query performance monitoring, and domain event handling. Perfect for building maintainable and scalable data access layers in modern .NET applications. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 8.0.0 | 178 | 11/9/2026 |
| 7.0.0 | 176 | 10/20/2026 |
| 6.5.2 | 156 | 10/10/2026 |
| 6.5.1 | 147 | 10/10/2026 |
| 6.5.0 | 158 | 10/9/2026 |
| 6.4.0 | 149 | 10/9/2026 |
| 6.3.0 | 148 | 10/8/2026 |
| 6.2.0 | 142 | 10/8/2026 |
| 6.1.0 | 152 | 10/7/2026 |
| 6.0.0 | 153 | 10/7/2026 |
| 5.2.1 | 153 | 10/7/2026 |
| 5.2.0 | 144 | 10/7/2026 |
| 5.1.0 | 156 | 10/7/2026 |
| 5.0.0 | 155 | 10/6/2026 |
| 4.1.0 | 154 | 10/6/2026 |
| 4.0.0 | 157 | 10/5/2026 |
| 3.2.3 | 380 | 5/31/2026 |
| 3.2.2 | 194 | 5/31/2026 |
| 3.2.1 | 191 | 5/31/2026 |
| 3.2.0 | 197 | 5/30/2026 |