EngineBay.Persistence
8.0.0
dotnet add package EngineBay.Persistence --version 8.0.0
NuGet\Install-Package EngineBay.Persistence -Version 8.0.0
<PackageReference Include="EngineBay.Persistence" Version="8.0.0" />
paket add EngineBay.Persistence --version 8.0.0
#r "nuget: EngineBay.Persistence, 8.0.0"
// Install EngineBay.Persistence as a Cake Addin #addin nuget:?package=EngineBay.Persistence&version=8.0.0 // Install EngineBay.Persistence as a Cake Tool #tool nuget:?package=EngineBay.Persistence&version=8.0.0
EngineBay.Persistence
Persistence module for EngineBay published to EngineBay.Persistence on NuGet.
About
The persistence module provides structures to configure and register any database connections that modules in your application might need.
The DbContexts from this module should be inherited by the DbContexts of any module that needs to use a database. To support Command and Query Responsibility Segregation (CQRS), a read-optimised and a write-optimised DbContext are provided - though a general-purpose one is also provided.
The TimestampInterceptor will add creation and modification timestamps to any model that implements EngineBay.Core's BaseModel.
The AuditableModel abstract class will provide some standard fields for tracking which users make changes to any inheriting models. If changes are saved on a DbContext that uses EngineBay.Auditing's AuditInterceptor, then audit entries will automatically be created for any models that implement AuditableModel.
ApplicationUser is a simple representation of a user for the application. It has a DbSet provided in ModuleDbContext so that any module inheriting these contexts will have access to the application users. ApplicationUser implements AuditableModel.
Usage
To use this module in your own, you will need to create three DbContexts - generic, read, and write - so that they can be registered. With the currently preferred structure, they will need to inherit from the Persistence DbContexts in a chain like shown in this diagram (using EngineBay.Blueprints module as an example):
%%{init: {"flowchart": {"htmlLabels": false}} }%%
flowchart BT
BlueprintsWriteDbContext --> BlueprintsQueryDbContext
BlueprintsQueryDbContext --> BlueprintsDbContext
BlueprintsDbContext --> ModuleWriteDbContext
ModuleWriteDbContext --> ModuleQueryDbContext
ModuleQueryDbContext --> ModuleDbContext
click ModuleDbContext "https://github.com/engine-bay/persistence/blob/feature/readmes/EngineBay.Persistence/DbContexts/ModuleDbContext.cs" _blank
click ModuleQueryDbContext "https://github.com/engine-bay/persistence/blob/feature/readmes/EngineBay.Persistence/DbContexts/ModuleQueryDbContext.cs" _blank
click ModuleWriteDbContext "https://github.com/engine-bay/persistence/blob/feature/readmes/EngineBay.Persistence/DbContexts/ModuleWriteDbContext.cs" _blank
click BlueprintsDbContext "https://github.com/engine-bay/blueprints/blob/main/EngineBay.Blueprints/Persistence/BlueprintsDbContext.cs" _blank
click BlueprintsQueryDbContext "https://github.com/engine-bay/blueprints/blob/main/EngineBay.Blueprints/Persistence/BlueprintsQueryDbContext.cs" _blank
click BlueprintsWriteDbContext "https://github.com/engine-bay/blueprints/blob/main/EngineBay.Blueprints/Persistence/BlueprintsWriteDbContext.cs" _blank
You should register your DbSets only in your generic DbContext:
namespace EngineBay.Blueprints
{
using EngineBay.Persistence;
using Microsoft.EntityFrameworkCore;
public class BlueprintsDbContext : ModuleWriteDbContext
{
public BlueprintsDbContext(DbContextOptions<ModuleWriteDbContext> options)
: base(options)
{
}
public DbSet<Workbook> Workbooks { get; set; } = null!;
public DbSet<Blueprint> Blueprints { get; set; } = null!;
// More DbSets...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Workbook.CreateDataAnnotations(modelBuilder);
Blueprint.CreateDataAnnotations(modelBuilder);
// More annotations...
base.OnModelCreating(modelBuilder);
}
}
}
If you desire extra functionality for any of the contexts, such as an auditing interceptor from EngineBay.Auditing, you can access the options builder with the OnConfiguring method, like this:
namespace EngineBay.Blueprints
{
using EngineBay.Auditing;
using EngineBay.Persistence;
using Microsoft.EntityFrameworkCore;
public class BlueprintsWriteDbContext : BlueprintsQueryDbContext
{
private readonly IAuditingInterceptor auditingInterceptor;
public BlueprintsWriteDbContext(DbContextOptions<ModuleWriteDbContext> options, IAuditingInterceptor auditingInterceptor)
: base(options)
{
this.auditingInterceptor = auditingInterceptor;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
ArgumentNullException.ThrowIfNull(optionsBuilder);
optionsBuilder.AddInterceptors(this.auditingInterceptor);
base.OnConfiguring(optionsBuilder);
}
}
}
When you've created your module's DbContexts, you will need to create a CQRSDatabaseConfiguration object in your module's setup class that will register the contexts. For example:
namespace EngineBay.Blueprints
{
using EngineBay.Core;
using EngineBay.Persistence;
using FluentValidation;
public class BlueprintsModule : BaseModule
{
public override IServiceCollection RegisterModule(IServiceCollection services, IConfiguration configuration)
{
// Other services registration...
var databaseConfiguration = new CQRSDatabaseConfiguration<BlueprintsDbContext, BlueprintsQueryDbContext, BlueprintsWriteDbContext>();
databaseConfiguration.RegisterDatabases(services);
return services;
}
// Other setup methods...
}
}
You will then be able to use your DbContexts freely, as you might any other DbContext, though do try to keep CQRS principals in mind.
Registration
This module cannot run on its own. You will need to register it in your application to use its functionality. See the Demo API registration guide.
Note that you do not need to register the Persistence DbContexts. The ApplicationUsers DbSet that these contexts provide will be available to any other module's DbContexts by virtue of inheritance.
Environment Variables
See the Documentation Portal.
Dependencies
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- EngineBay.Core (>= 10.0.0)
- Humanizer.Core (>= 2.14.1)
- LinqKit.Microsoft.EntityFrameworkCore (>= 8.1.5)
- Microsoft.EntityFrameworkCore (>= 8.0.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 8.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 8.0.0)
- Microsoft.EntityFrameworkCore.SqlServer (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 8.0.0)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on EngineBay.Persistence:
Package | Downloads |
---|---|
EngineBay.Blueprints
Package Description |
|
EngineBay.Authentication
Package Description |
|
EngineBay.Auditing
Package Description |
|
EngineBay.DatabaseManagement
Package Description |
|
EngineBay.DemoModule
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
8.0.0 | 443 | 2/12/2024 |
7.0.2 | 287 | 1/16/2024 |
7.0.1 | 146 | 1/10/2024 |
7.0.0 | 192 | 1/9/2024 |
6.0.8 | 115 | 1/3/2024 |
6.0.7 | 86 | 1/3/2024 |
6.0.6 | 93 | 1/3/2024 |
6.0.5 | 120 | 12/21/2023 |
6.0.4 | 422 | 12/20/2023 |
6.0.3 | 514 | 12/6/2023 |
6.0.2 | 114 | 11/29/2023 |
6.0.1 | 308 | 11/23/2023 |
6.0.0 | 142 | 11/23/2023 |
5.0.7 | 106 | 11/20/2023 |
5.0.6 | 110 | 11/17/2023 |
5.0.5 | 345 | 11/15/2023 |
5.0.4 | 106 | 11/9/2023 |
5.0.3 | 96 | 11/9/2023 |
5.0.2 | 95 | 11/9/2023 |
5.0.1 | 257 | 11/2/2023 |
5.0.0 | 542 | 10/31/2023 |
4.2.1 | 103 | 10/31/2023 |
4.2.0 | 392 | 10/24/2023 |
4.1.4 | 122 | 10/11/2023 |
4.1.3 | 106 | 10/10/2023 |
4.1.2 | 118 | 10/9/2023 |
4.1.1 | 227 | 9/28/2023 |
4.1.0 | 424 | 9/28/2023 |
4.0.0 | 211 | 9/28/2023 |
3.4.12 | 116 | 9/25/2023 |
3.4.11 | 111 | 9/25/2023 |
3.4.10 | 107 | 9/25/2023 |
3.4.9 | 106 | 9/24/2023 |
3.4.8 | 300 | 9/24/2023 |
3.4.7 | 111 | 9/24/2023 |
3.4.6 | 164 | 9/23/2023 |
3.4.5 | 105 | 9/23/2023 |
3.4.4 | 116 | 9/23/2023 |
3.4.3 | 108 | 9/22/2023 |
3.4.2 | 109 | 9/21/2023 |
3.4.1 | 216 | 6/2/2023 |
3.4.0 | 125 | 6/1/2023 |
3.3.18 | 134 | 5/31/2023 |
3.3.17 | 121 | 5/31/2023 |
3.3.16 | 130 | 5/31/2023 |
3.3.15 | 576 | 4/25/2023 |
3.3.14 | 160 | 4/24/2023 |
3.3.13 | 236 | 4/21/2023 |
3.3.12 | 153 | 4/17/2023 |
3.3.11 | 157 | 4/17/2023 |
3.3.10 | 152 | 4/17/2023 |
3.3.9 | 224 | 4/12/2023 |
3.3.8 | 165 | 4/12/2023 |
3.3.7 | 167 | 4/8/2023 |
3.3.6 | 585 | 4/3/2023 |
3.3.5 | 172 | 4/3/2023 |
3.3.4 | 256 | 4/2/2023 |
3.3.3 | 187 | 4/1/2023 |
3.3.2 | 192 | 4/1/2023 |
3.3.1 | 181 | 4/1/2023 |
3.3.0 | 1,050 | 3/29/2023 |
3.2.5 | 487 | 3/27/2023 |
3.2.4 | 260 | 3/26/2023 |
3.2.3 | 189 | 3/26/2023 |
3.2.2 | 191 | 3/26/2023 |
3.2.1 | 181 | 3/26/2023 |
3.2.0 | 790 | 3/26/2023 |
3.1.0 | 422 | 3/26/2023 |
3.0.1 | 194 | 3/25/2023 |
3.0.0 | 1,151 | 3/25/2023 |
2.5.2 | 971 | 3/24/2023 |
2.5.1 | 265 | 3/24/2023 |
2.5.0 | 276 | 3/23/2023 |
2.4.0 | 198 | 3/23/2023 |
2.3.1 | 200 | 3/23/2023 |
2.3.0 | 276 | 3/23/2023 |
2.2.0 | 185 | 3/22/2023 |
2.1.2 | 285 | 3/22/2023 |
2.1.1 | 348 | 3/22/2023 |
2.0.1 | 267 | 3/21/2023 |
2.0.0 | 229 | 3/21/2023 |
1.3.0 | 262 | 3/21/2023 |
1.2.0 | 239 | 3/21/2023 |
1.1.1 | 277 | 3/20/2023 |
1.1.0 | 259 | 3/20/2023 |
1.0.3 | 192 | 3/20/2023 |
1.0.2 | 222 | 3/19/2023 |
1.0.1 | 204 | 3/19/2023 |
1.0.0 | 204 | 3/19/2023 |
0.10.2 | 220 | 3/19/2023 |
0.10.1 | 180 | 3/19/2023 |
0.10.0 | 190 | 3/19/2023 |
0.9.0 | 203 | 3/19/2023 |
0.8.0 | 206 | 3/19/2023 |
0.7.3 | 203 | 3/19/2023 |
0.7.2 | 195 | 3/19/2023 |
0.7.1 | 198 | 3/19/2023 |
0.7.0 | 385 | 3/19/2023 |
0.6.0 | 246 | 3/19/2023 |
0.5.0 | 276 | 3/18/2023 |
0.4.1 | 203 | 3/18/2023 |
0.4.0 | 254 | 3/18/2023 |
0.3.0 | 210 | 3/18/2023 |
0.2.1 | 209 | 3/18/2023 |
0.2.0 | 211 | 3/18/2023 |
0.1.0 | 216 | 3/17/2023 |
0.0.1 | 219 | 3/17/2023 |