Forge.TransactionHandler 1.0.3

dotnet add package Forge.TransactionHandler --version 1.0.3                
NuGet\Install-Package Forge.TransactionHandler -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Forge.TransactionHandler" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Forge.TransactionHandler --version 1.0.3                
#r "nuget: Forge.TransactionHandler, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

image

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 107 5/7/2024
1.0.2 97 5/7/2024
1.0.1 96 5/7/2024
1.0.0 102 5/6/2024