GM.EntityFramework.Persistence
1.2.1
dotnet add package GM.EntityFramework.Persistence --version 1.2.1
NuGet\Install-Package GM.EntityFramework.Persistence -Version 1.2.1
<PackageReference Include="GM.EntityFramework.Persistence" Version="1.2.1" />
<PackageVersion Include="GM.EntityFramework.Persistence" Version="1.2.1" />
<PackageReference Include="GM.EntityFramework.Persistence" />
paket add GM.EntityFramework.Persistence --version 1.2.1
#r "nuget: GM.EntityFramework.Persistence, 1.2.1"
#:package GM.EntityFramework.Persistence@1.2.1
#addin nuget:?package=GM.EntityFramework.Persistence&version=1.2.1
#tool nuget:?package=GM.EntityFramework.Persistence&version=1.2.1
<p align="center"> <img src="https://raw.githubusercontent.com/gmetskhvarishvili/GM.EntityFramework/master/icon.png" alt="GM.EntityFramework" width="140" height="140" /> </p>
GM.EntityFramework
A lightweight DDD + Entity Framework Core toolkit for .NET: base entities, value objects, the specification pattern, a generic repository, and a unit of work with transactions and automatic auditing. Targets .NET 10.
Packages
The three packages version and release together (lockstep):
| Package | What it gives you |
|---|---|
GM.EntityFramework.Domain |
Entities, value objects, specifications, results, domain-event and repository abstractions (no EF dependency to reference in your domain layer beyond EF Core primitives). |
GM.EntityFramework.Persistence |
The EF Core implementation: GenericRepository, GenericUnitOfWork, and an auditing GenericDbContext. |
GM.EntityFramework |
Meta-package that pulls in both of the above. |
dotnet add package GM.EntityFramework
Quick start
1. Define entities
using GM.EntityFramework.Domain.Base;
public class Product : AuditableEntity<Guid> // adds CreatedAt / UpdatedAt (set automatically)
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
Base classes: Entity<TKey> → AuditableEntity<TKey> (timestamps) →
SoftDeletableEntity<TKey> (adds IsActive / IsDeleted / IsHidden with SoftRemove(),
RestoreAll(), etc.). There's also ValueObject for equality-by-value types.
2. Create a DbContext
Inherit GenericDbContext — it stamps CreatedAt/UpdatedAt on save automatically.
using GM.EntityFramework.Persistence;
using Microsoft.EntityFrameworkCore;
public class AppDbContext(DbContextOptions options) : GenericDbContext(options)
{
public DbSet<Product> Products => Set<Product>();
}
3. Register and use the repository + unit of work
services.AddDbContext<AppDbContext>(o => o.UseSqlServer(connectionString));
services.AddScoped<IGenericUnitOfWork, GenericUnitOfWork<AppDbContext>>();
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<,>));
await repository.AddAsync(new Product { Name = "Book", Price = 9.99m });
await unitOfWork.SaveChangesAsync();
var cheap = await repository.FindAsync(p => p.Price < 10);
var exists = await repository.ExistsAsync(p => p.Name == "Book");
4. Specifications
Encapsulate query logic and compose it with And / Or / Not:
using GM.EntityFramework.Domain.Specifications;
public class CheapProductsSpec : BaseSpecification<Product>
{
public CheapProductsSpec()
{
AddCriteria(p => p.Price < 10);
ApplyOrdering(nameof(Product.Name));
ApplyPaging(currentPage: 1, pageSize: 20);
}
}
var page = await repository.ListAsync(new CheapProductsSpec());
5. Transactions
await unitOfWork.ExecuteInTransactionAsync(async () =>
{
await repository.AddAsync(product);
// ... more work; commits on success, rolls back on exception
});
Concurrency conflicts surface as a ConcurrencyException.
Sample application
A complete, runnable DDD + CQRS example built on these packages lives in a separate repo: GM.EntityFramework.Samples. It's a layered ASP.NET Core Web API (Domain / Application / Persistence / API) that shows how the pieces fit together in a real project:
- A
Sampleaggregate root (SoftDeletableEntity<int>,IAggregateRoot) with childSampleItementities, factory methods, and encapsulated mutations. - A custom
ISampleRepository : IGenericRepository<Sample>, implemented by deriving fromGenericRepository<Sample, ApplicationDbContext>. - An
ApplicationDbContext : GenericDbContextthat applies EF configurations from the assembly and gets automaticCreatedAt/UpdatedAtauditing for free. - A
UnitOfWork : GenericUnitOfWork<ApplicationDbContext>that exposes the aggregate repository. - CQRS commands (create / update / delete) and queries (details / list), EF Core migrations, and a PostgreSQL (Npgsql) provider.
Clone it to see the library used end-to-end.
Repository layout
GM.EntityFramework/
├── GM.EntityFramework.Domain/ # abstractions (entities, specifications, results, events)
├── GM.EntityFramework.Persistence/ # EF Core implementation (repository, UoW, DbContext)
├── GM.EntityFramework/ # meta-package
└── tests/GM.EntityFramework.Tests/ # xUnit tests (SQLite in-memory for persistence)
Building & testing
dotnet build -c Release
dotnet test -c Release
Releasing
Versioning is automated from Conventional Commits —
see CONTRIBUTING.md. All three packages share one version
(Directory.Build.props) and publish together to nuget.org on each release.
License
MIT — see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- GM.EntityFramework.Domain (>= 1.2.1)
NuGet packages (9)
Showing the top 5 NuGet packages that depend on GM.EntityFramework.Persistence:
| Package | Downloads |
|---|---|
|
GM.EntityFramework
A DDD + EF Core toolkit for .NET. This meta-package bundles GM.EntityFramework.Domain and GM.EntityFramework.Persistence. |
|
|
GM.Notifications.Domain
Notification domain model for GM.Notifications — the NotificationBase aggregate (status, retry, scheduling) and per-channel entities (email, SMS, WhatsApp, push, Slack) built on EF Core. |
|
|
GM.Notifications.Push
Push notification channel for GM.Notifications — IPushSenderService, options, and DI wiring (AddPushNotificationServices). |
|
|
GM.Notifications.Email
Email notification channel for GM.Notifications — IEmailSenderService, options, and DI wiring (AddEmailNotificationServices). |
|
|
GM.Notifications.Persistence
EF Core persistence for GM.Notifications — entity type configurations mapping the notification entities to a relational (PostgreSQL) schema. |
GitHub repositories
This package is not used by any popular GitHub repositories.