MiCake 11.0.0

Prefix Reserved
dotnet add package MiCake --version 11.0.0
                    
NuGet\Install-Package MiCake -Version 11.0.0
                    
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="MiCake" Version="11.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MiCake" Version="11.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MiCake" />
                    
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 MiCake --version 11.0.0
                    
#r "nuget: MiCake, 11.0.0"
                    
#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 MiCake@11.0.0
                    
#: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=MiCake&version=11.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MiCake&version=11.0.0
                    
Install as a Cake Tool

MiCake

A lightweight Domain-Driven Design (DDD) toolkit for .NET.

Overview

MiCake is the main DDD package providing all essential components:

  • Entity & Aggregate Root - DDD tactical pattern implementations
  • Value Objects - Immutable value type support
  • Repository Pattern - Data access abstraction
  • Domain Events - Event-driven domain modeling
  • Unit of Work - Transaction management (the sole persistence owner)
  • Audit Support - Automatic timestamp tracking

Installation

dotnet add package MiCake

Quick Start

// Define your aggregate root
public class Order : AggregateRoot<Guid>
{
    public string CustomerName { get; private set; }
    public decimal TotalAmount { get; private set; }
    
    public void UpdateTotal(decimal amount)
    {
        TotalAmount = amount;
        AddDomainEvent(new OrderUpdatedEvent(Id));
    }
}

Key Components

Component Description
Entity<TKey> Base class for domain entities
AggregateRoot<TKey> Base class for aggregate roots
ValueObject Base class for value objects
IRepository<T> Repository interface for data access
IDomainEvent Interface for domain events

Unit of Work

The unit of work is the sole persistence owner. Repositories track changes but never save or commit independently; persistence happens when the ambient unit of work flushes and commits.

Basic Writable Flow

public class BookService
{
    private readonly IRepository<Book, Guid> _bookRepository;
    private readonly IUnitOfWorkManager _uowManager;

    public async Task<Guid> CreateBookAsync(string name)
    {
        using var uow = await _uowManager.BeginAsync();

        var book = new Book(name);
        await _bookRepository.AddAsync(book);

        // Explicit flush is only required when the database generates the key.
        // If the key is application-generated, it is already available on the instance.
        await uow.FlushAsync();

        await uow.CommitAsync(); // Dispatches domain events and commits the transaction
        return book.Id;
    }
}
  • AddAsync tracks the aggregate; it does not persist.
  • FlushAsync writes tracked changes without committing and returns the affected row count.
  • CommitAsync flushes, dispatches domain events, and commits the transaction.
  • RollbackAsync rolls back every participating resource.

Read-Only Unit of Work

Read-only units of work reject resource flush and write activation — every attempted write fails before a command executes:

var uow = await _uowManager.BeginAsync(UnitOfWorkOptions.ReadOnly);

Choosing an Execution Mode

Intent Ambient UoW at call site Entry
Own a transaction boundary (host edge, long-running flow) any BeginAsync (a nested BeginAsync shares the root when an ambient UoW exists)
Isolated committed write block unknown — entry-agnostic service ExecuteIsolatedAsync (recommended default)
Isolated committed write block always present; misuse must fail fast ExecuteRequiresNewAsync (strict — throws without an ambient UoW)
Isolated committed write block never present; misuse must fail fast IStandaloneUnitOfWorkExecutor (strict — throws with an ambient UoW)

The two strict entries are wiring self-check variants of ExecuteIsolatedAsync: their preconditions turn misuse into an immediate, diagnosable failure.

Context-Agnostic Isolated Execution (ExecuteIsolatedAsync)

ExecuteIsolatedAsync runs the callback in a fully isolated DI scope with its own root unit of work regardless of any ambient unit of work. With an ambient UoW it is suspended and restored on every exit path (success, failure, cancellation, commit failure, rollback failure); without one, restoration is a no-op. It commits on success and rolls back on failure — the recommended default entry for isolated write blocks in any host:

await uowManager.ExecuteIsolatedAsync(async (provider, ct) =>
{
    var repo = provider.GetRequiredService<IRepository<Order, Guid>>();
    await repo.AddAsync(new Order(...), ct);
    // Commits on success, rolls back on failure
}, options: null, cancellationToken: ct);

Isolated Execution (requiresNew, strict)

Use callback-based APIs instead of the removed requiresNew boolean overloads. ExecuteRequiresNewAsync runs the callback in a fully isolated DI scope with its own root unit of work, and restores the previous ambient unit of work afterwards. As a wiring self-check it requires an ambient UoW — use ExecuteIsolatedAsync when the same call site must also run without one:

await uowManager.ExecuteRequiresNewAsync(async (provider, ct) =>
{
    var repo = provider.GetRequiredService<IRepository<Order, Guid>>();
    await repo.AddAsync(new Order(...), ct);
    // The inner unit of work commits on success and rolls back on failure
}, options: null, cancellationToken: ct);

ExecuteRequiresNewAsync requires a live outer unit of work; without one it throws InvalidOperationException naming the remediation paths (BeginAsync boundary frame, ExecuteIsolatedAsync, or IStandaloneUnitOfWorkExecutor).

Standalone Execution (strict)

IStandaloneUnitOfWorkExecutor executes an operation without any ambient unit of work, in its own DI scope with a root writable unit of work. As a wiring self-check it rejects an existing ambient unit of work to keep its contract unambiguous — use ExecuteIsolatedAsync when the same call site must also run with one:

var executor = provider.GetRequiredService<IStandaloneUnitOfWorkExecutor>();
await executor.ExecuteAsync(async (isolatedProvider, ct) =>
{
    var repo = isolatedProvider.GetRequiredService<IRepository<Order, Guid>>();
    await repo.AddAsync(new Order(...), ct);
});

Savepoints

Root units of work expose savepoint management for partial rollback:

var name = await uow.CreateSavepointAsync("checkpoint");
await uow.RollbackToSavepointAsync(name);
await uow.ReleaseSavepointAsync(name);

Multi-Resource Semantics

Resources are committed in registration order. If a resource fails, already committed resources remain committed, later resources are rolled back, and a PartialUnitOfWorkCommitException carries the complete per-resource outcome together with the original failures. Best-effort semantics: use a single provider per unit of work unless you accept partial commit.

Migration Guide

The following APIs were removed or changed. Replace them as shown:

Removed / Changed API Replacement
IRepository.AddAndReturnAsync(...) IRepository.AddAsync(...) followed by IUnitOfWork.FlushAsync() where a generated key is required
IRepository.SaveChangesAsync() IUnitOfWork.CommitAsync() on the ambient unit of work (or FlushAsync() to write without committing)
IRepository.ClearChangeTrackingAsync() Remove the instance from tracking state; the unit of work owns the change tracker
IUnitOfWorkManager.BeginAsync(options, requiresNew: true) IUnitOfWorkManager.ExecuteRequiresNewAsync(callback, ...)
PersistenceStrategy Removed. Every writable unit of work uses explicit transactions; read-only units of work are the only non-transactional mode
UnitOfWorkOptions.Timeout Removed. It previously had no runtime effect
IDbContextWrapper IUnitOfWorkResource (provider integration contract)

Documentation

📚 Full Documentation

License

MIT License - see LICENSE

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on MiCake:

Package Downloads
MiCake.EntityFrameworkCore

EF Core integration utilities for MiCake.

MiCake.AspNetCore

ASP.NET Core integration extensions for MiCake.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.0 43 11/26/2026
11.0.0-preview.2611260249 52 11/26/2026
11.0.0-preview.2610080956 85 10/8/2026
11.0.0-preview.2608081400 125 8/8/2026
11.0.0-preview.2608081123 113 8/8/2026
11.0.0-preview.12 106 8/8/2026
10.0.0 189 5/11/2026
10.0.0-preview.11 167 3/16/2026
10.0.0-preview.10 114 2/23/2026
10.0.0-preview.9 134 2/9/2026
10.0.0-preview.8 116 1/21/2026
10.0.0-preview.7 109 1/20/2026
10.0.0-preview.5 121 1/15/2026
1.0.0-CI-20251227-143701 181 12/27/2025
1.0.0-CI-20251227-131439 140 12/27/2025
1.0.0-CI-20251203-144406 720 12/3/2025
1.0.0-CI-20251202-144550 713 12/2/2025
1.0.0-CI-20251128-083448 200 11/28/2025
1.0.0-CI-20251123-045010 190 11/23/2025
0.9.0-CI-20251107-105430 211 11/7/2025
Loading failed