Forge.TransactionHandler
1.0.3
dotnet add package Forge.TransactionHandler --version 1.0.3
NuGet\Install-Package Forge.TransactionHandler -Version 1.0.3
<PackageReference Include="Forge.TransactionHandler" Version="1.0.3" />
paket add Forge.TransactionHandler --version 1.0.3
#r "nuget: Forge.TransactionHandler, 1.0.3"
// Install Forge.TransactionHandler as a Cake Addin #addin nuget:?package=Forge.TransactionHandler&version=1.0.3 // Install Forge.TransactionHandler as a Cake Tool #tool nuget:?package=Forge.TransactionHandler&version=1.0.3
Forge.TransactionHandler
Quickstart
Once the package is installed you can get started by first adding a transaction group to your application's service container:
builder.Services.AddTransactionGroup(
group => group
.UseName("Your group name")
.Add<YourDbContext>()
);
The next step is to add the transaction handler to the middleware pipeline:
app.UseTransactionHandler();
And thats add changes will now automatically be persisted to your DbContext
if the status code of the response was ⇐299 and >=200
Transaction Groups
A transaction group can contain as many DbContexts
as you want:
builder.Services.AddTransactionGroup(
group => group
.UseName("Your group name")
.Add<YourDbContext>()
.Add<YourSecondDbContext>()
.Add<YourThirdDbContext>()
);
The purpose of the transaction group is to save changes on all contexts in that group in one database transaction.
As in if one fails they all fail and only if all of them succeed are changes persisted.
Transaction groups can be used seperately from the transaction handler.
If a transaction group is required inside a service it can be resolved like so:
class MyService()
{
readonly ITransactionGroup _myGroup;
public MyService([ResolveTransactionGroup("Your group name")] ITransactionGroup myGroup)
{
_myGroup = myGroup
}
}
Transaction Handler
The transaction handler can be configured to persist changes based on any condition (the default being if the request was successful).
This can be done like so:
app.UseTransactionHandler(config =>
{
return config.HandleTransactionGroup(
"Your group",
group => group.SaveIf(context => context.User.IsInRole("Admin"))
.And(context => context.Response.Headers.ContainsKey("Persist"))
.Or(context => context.Response.StatusCode >= 400)
);
});
This is using explicit configuration of the transaction handler if this is done all transaction groups must be added in this way
The transaction handler can also be disabled for any given endpoint or controller like so:
[HttpGet]
[DisableTransactionHandler]
public SomeResponse Get()
{
...
}
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
- Microsoft.AspNetCore.Routing.Abstractions (>= 2.2.0)
- Microsoft.EntityFrameworkCore (>= 8.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.