Tooark.Mediator.EntityFrameworkCore 4.7.1

dotnet add package Tooark.Mediator.EntityFrameworkCore --version 4.7.1
                    
NuGet\Install-Package Tooark.Mediator.EntityFrameworkCore -Version 4.7.1
                    
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="Tooark.Mediator.EntityFrameworkCore" Version="4.7.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tooark.Mediator.EntityFrameworkCore" Version="4.7.1" />
                    
Directory.Packages.props
<PackageReference Include="Tooark.Mediator.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tooark.Mediator.EntityFrameworkCore --version 4.7.1
                    
#r "nuget: Tooark.Mediator.EntityFrameworkCore, 4.7.1"
                    
#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.
#:package Tooark.Mediator.EntityFrameworkCore@4.7.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tooark.Mediator.EntityFrameworkCore&version=4.7.1
                    
Install as a Cake Addin
#tool nuget:?package=Tooark.Mediator.EntityFrameworkCore&version=4.7.1
                    
Install as a Cake Tool

Tooark.Mediator.EntityFrameworkCore

Library that moves Entity Framework Core persistence into the Tooark.Mediator pipeline, keeping handlers free of SaveChanges.

πŸ“– Docs: Tooark.Mediator.EntityFrameworkCore on the site Β· All packages Β· API reference

🌍 Languages: πŸ‡ΊπŸ‡Έ English (this file) Β· πŸ‡§πŸ‡· PortuguΓͺs


πŸ“‘ Contents


πŸ“– Overview

Every command handler ends with await context.SaveChangesAsync(...). Repeated across dozens of handlers, the line becomes noise β€” and forgetting it produces a command that "works" without writing anything.

The package registers a behavior in the Tooark.Mediator pipeline that persists the changes once, at the end of the command:

  • handlers only describe the changes on the DbContext;
  • persistence happens only when the pipeline completes without an exception;
  • queries do not go through persistence;
  • nested commands take part in the same unit of work.

πŸ”§ Installation

dotnet add package Tooark.Mediator.EntityFrameworkCore

βš™οΈ Configuration

using Tooark.Mediator.EntityFrameworkCore.Injections;
using Tooark.Mediator.Injections;

builder.Services.AddDbContext<AppDbContext>(options => options.UseNpgsql(connectionString));

builder.Services.AddTooarkMediator(typeof(Program).Assembly);
builder.Services.AddTooarkMediatorUnitOfWork<AppDbContext>();

With an explicit transaction, for when the handler persists more than once or combines the DbContext with another transactional resource:

using Tooark.Mediator.EntityFrameworkCore.Enums;

builder.Services.AddTooarkMediatorUnitOfWork<AppDbContext>(EUnitOfWorkStrategy.Transaction);

Registration order: the behavior takes the position where AddTooarkMediatorUnitOfWork is called, and the pipeline behaviors run in registration order. Register validation before the unit of work β€” it makes no sense to prepare persistence and then reject the request.


πŸ“¦ Components

Persistence strategies

Strategy Behavior
SaveChanges Persists at the end of the command, with Entity Framework Core's implicit transaction
Transaction Runs the command inside an explicit transaction, persisting before committing

SaveChanges is the default and covers the common case: a single call to SaveChangesAsync is already atomic, since Entity Framework Core wraps the batch in a transaction. The explicit transaction is only needed when there is more than one persistence in the same command, or when the DbContext is combined with another transactional resource.

In the Transaction strategy, the transaction is opened inside the provider's resiliency strategy. That avoids the error that occurs when opening an explicit transaction with retries configured (EnableRetryOnFailure), but it means the operation may run more than once on a transient failure β€” the handler must be idempotent.

Dependency injection

  • TooarkDependencyInjection.AddTooarkMediatorUnitOfWork<TContext>(IServiceCollection, EUnitOfWorkStrategy)

Abstraction

  • IUnitOfWork (Tooark.Mediator.EntityFrameworkCore.Interfaces): exposes SaveChangesAsync and ExecuteInTransactionAsync, with no Entity Framework types, allowing the implementation to be replaced in tests.

Behavior with nested commands

A command dispatched from within another command takes part in the unit of work already started: only the outermost command persists. Without that control, the inner command would write in the middle of the outer one's operation, and a later failure would leave the database in a partial state.

Behavior with notifications

Notifications do not go through the pipeline: their handlers run inside the command handler, therefore before persistence. Whatever they write to the DbContext is saved together, in the same unit of work.


πŸ“ Usage Examples

Handler without persistence

using Tooark.Mediator.Abstractions;
using Tooark.Mediator.Handlers;

public sealed record CreateProduct(string Name) : ICommand<Guid>;

public sealed class CreateProductHandler(AppDbContext context) : ICommandHandler<CreateProduct, Guid>
{
  public Task<Guid> HandleAsync(CreateProduct request, CancellationToken cancellationToken = default)
  {
    var product = new Product(request.Name);

    context.Products.Add(product);

    // No SaveChanges: persistence is the pipeline's responsibility
    return Task.FromResult(product.Id);
  }
}

Query, which does not go through persistence

public sealed record CountProducts : IQuery<int>;

public sealed class CountProductsHandler(AppDbContext context) : IQueryHandler<CountProducts, int>
{
  public Task<int> HandleAsync(CountProducts request, CancellationToken cancellationToken = default)
  {
    return context.Products.CountAsync(cancellationToken);
  }
}

The behavior is constrained to ICommand<TResponse>, so the container skips it when dispatching the query β€” with no runtime type check.

Full pipeline

builder.Services.AddTooarkMediator(typeof(Program).Assembly);
builder.Services.AddTooarkMediatorBehavior(typeof(LoggingBehavior<,>));
builder.Services.AddTooarkMediatorBehavior(typeof(ValidationBehavior<,>));
builder.Services.AddTooarkMediatorUnitOfWork<AppDbContext>();

From outermost to innermost: logging, validation, unit of work and, last, the handler.


πŸ“‹ Dependencies

Package Version Usage
Tooark.Mediator 4.x Behavior pipeline and registration
Tooark.Mediator.Abstractions 4.x Message contracts (ICommand)
Tooark.Exceptions 4.x Configuration errors
Microsoft.EntityFrameworkCore 8.x/10.x Context, persistence and transactions
Microsoft.Extensions.DependencyInjection.Abstractions 8.x/10.x Container registration

The package is part of the Tooark aggregator, so whoever installs Tooark already gets it.


🀝 Contributing

Contributions are welcome! Start with CONTRIBUTING.md β€” it covers the development workflow, the coding and commit conventions and the pull request checklist. Bugs and feature requests go through the issue templates of the Tooark repository.

By participating you agree to the Code of Conduct.


πŸ†˜ Help & Security

  • ❓ Questions, bugs, feature ideas β€” see SUPPORT.md for the right channel
  • πŸ”’ Security vulnerabilities β€” do not open a public issue; follow SECURITY.md

πŸ’– Support

If Tooark helps your projects, consider supporting its development:

Every contribution helps keep the project maintained and improving. Thank you! πŸ™


πŸ“„ License

This project is licensed under the BSD 3-Clause License.

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

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Tooark.Mediator.EntityFrameworkCore:

Package Downloads
Tooark

Package with all Tooark resources for .NET applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.7.1 0 10/17/2026
4.7.0 250 9/30/2026
4.6.0 142 9/29/2026
4.5.0 103 9/29/2026
4.4.0 106 9/28/2026
4.3.0 116 9/26/2026
4.2.1 121 9/25/2026
4.2.0 119 9/20/2026
4.1.0 120 9/18/2026
4.0.1 159 8/25/2026
4.0.0 122 8/25/2026