Koto.Domain 0.5.0-preview.1

This is a prerelease version of Koto.Domain.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Koto.Domain --version 0.5.0-preview.1
                    
NuGet\Install-Package Koto.Domain -Version 0.5.0-preview.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="Koto.Domain" Version="0.5.0-preview.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Koto.Domain" Version="0.5.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="Koto.Domain" />
                    
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 Koto.Domain --version 0.5.0-preview.1
                    
#r "nuget: Koto.Domain, 0.5.0-preview.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 Koto.Domain@0.5.0-preview.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=Koto.Domain&version=0.5.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Koto.Domain&version=0.5.0-preview.1&prerelease
                    
Install as a Cake Tool

Koto.Domain

DDD building blocks for .NET microservices. Zero external dependencies.

Install

dotnet add package Koto.Domain

What's included

Type Purpose
AggregateRoot<TId> Base class that collects domain events
Entity<TId> Identity-based equality (transient entities are never equal)
ValueObject Component-based equality
StronglyTypedId<T> Typed wrappers for Guid / int / string IDs
Error Abstract record — every error is a record type from a catalog; its properties are the data the error guarantees
ErrorCode Validates codes: context.entity.problem, at least three kebab-case segments
Errors.General Shared catalog: ValueIsRequired, InvalidLength, NotFound, CollectionIsTooSmall, CollectionIsTooLarge
Result<T> A value or an ErrorList<Error>; FirstError is the headline, Errors is the whole list
ErrorList<TError> Immutable error list that converts implicitly into a result: return other.Errors;
Result (static) Success() / Failure(…) for void flows, Combine, Sequence, Traverse
IDomainEvent / DomainEvent Event interfaces and base record (init properties survive JSON round-trips)
Unit Return type for commands that produce no value

IRepository<TAgg, TId> lives in Koto.Application (next to IUnitOfWork) — the port belongs to the layer that consumes it (handlers); the domain stays persistence-free.

Errors are types

// Identity.Domain/Errors/OtpErrors.cs — the catalog reads as the contract
public sealed record OtpExpired(DateTimeOffset ExpiredAt)
    : Error("identity.otp.expired", "The code has expired.");

public sealed record OtpTooManyAttempts(int RetryAfterSeconds)
    : Error("identity.otp.too-many-attempts", "Too many attempts. Try again later.");
  • There is no new Error(code, message): Error is abstract. Define the record in the layer that detects the condition — domain, application or infrastructure.
  • The record's properties are the error's guaranteed data. They reach clients as details ({ "retryAfterSeconds": 42 }), and C# code can pattern-match on the type.
  • Codes are validated on construction (ErrorCode.Pattern): orders.order.not-found, general.value.is-required. Koto.Testing's ErrorCatalog.ShouldBeValid(assembly) checks the format and the uniqueness of codes across the whole catalog.
  • Shared errors are created the same way: new Errors.General.NotFound("order", id).

Usage

Value Object

public sealed record Email
{
    public string Value { get; }
    private Email(string value) => Value = value;

    public static Result<Email> Create(string value) =>
        string.IsNullOrWhiteSpace(value) ? new Errors.General.ValueIsRequired() :
        value.Length > 150              ? new Errors.General.InvalidLength(1, 150) :
                                          new Email(value);
}

Aggregate Root

public sealed record OrderId(Guid Value) : StronglyTypedId<Guid>(Value)
{
    public static OrderId New() => new(Guid.NewGuid());
}

public sealed record OrderAlreadyCancelled(OrderId OrderId)
    : Error("orders.order.already-cancelled", "The order is already cancelled.");

public class Order : AggregateRoot<OrderId>
{
    public static Result<Order> Place(IReadOnlyList<OrderItem> items)
    {
        if (items.Count == 0)
            return new Errors.General.CollectionIsTooSmall(1, 0);

        var order = new Order(OrderId.New());
        order.AddDomainEvent(new OrderPlacedDomainEvent(order.Id));
        return order;
    }

    public Result<Unit> Cancel(string reason)
    {
        if (Status == OrderStatus.Cancelled)
            return new OrderAlreadyCancelled(Id);

        Status = OrderStatus.Cancelled;
        AddDomainEvent(new OrderCancelledDomainEvent(Id, reason));
        return Unit.Value;
    }
}

Propagate all errors, not the first

var email = Email.Create(dto.Email);
if (email.IsFailure)
    return email.Errors;        // every error travels on — as short as it gets

// Not this: `return email.FirstError;` keeps one error of N. FirstError is for display and
// for the HTTP status, not for propagation.

Combine several factories (all errors are collected)

var fields = Result.Combine(Email.Create(dto.Email), Name.Create(dto.Name));
if (fields.IsFailure)
    return fields.Errors;

var (email, name) = fields.Value;

Fold collections

Result<IReadOnlyList<Money>> prices = Result.Traverse(lines, l => Money.Create(l.Amount));
var jumps = await Result.TraverseAsync(request.Jumps, ResolveJumpAsync, FailureMode.FailFast, ct);

Match

return result.Match(
    onSuccess: order => Ok(order),
    onFailure: errors => Problem(errors[0].Message, errors.Count));

Upgrading from 0.4

0.5 makes Error abstract — every error is a record in a catalog — and renames Result<T>.Error to FirstError; Errors is an ErrorList<Error>, TapError, IDomainService and IResultFactory are gone, the Errors.General codes changed. The step-by-step guide for the whole suite, with before/after code, is docs/migrations/0.4-to-0.5.md.

License

MIT

Product Compatible and additional computed target framework versions.
.NET 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.
  • net10.0

    • No dependencies.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on Koto.Domain:

Package Downloads
Koto.Application

CQRS dispatcher, pipeline behaviors, and cross-service integration abstractions for .NET microservices. Depends only on Koto.Domain and Microsoft.Extensions abstractions.

Koto.Validation

FluentValidation extensions for Koto: MustBeValueObject, ListMustContainNumberOfItems, the ValidationBehavior pipeline behavior (enriched errors per field) and AddValidationBehavior ordering.

Koto.EventSourcing.Marten

Event Sourcing on PostgreSQL via Marten: event-sourced repository, MartenUnitOfWork, inline and async projections. The aggregate base (EventSourcedAggregateRoot) lives in Koto.Domain, so domain projects never reference this package.

Koto.Infrastructure.Http

Anti-Corruption Layer for HTTP calls between services: typed ServiceHttpClient base, Result-mapped error handling, correlation ID propagation, and standard resilience.

Koto.Infrastructure.EFCore

EF Core 10 integration for Koto DDD: generic repository, strongly-typed ID converters, pagination, and Wolverine outbox wiring.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.5.0-preview.9 73 11/30/2026
0.5.0-preview.8 80 11/30/2026
0.5.0-preview.7 76 11/29/2026
0.5.0-preview.6 75 11/29/2026
0.5.0-preview.5 81 11/29/2026
0.5.0-preview.4 86 11/29/2026
0.5.0-preview.3 82 11/29/2026
0.5.0-preview.2 83 11/29/2026
0.5.0-preview.1 117 11/8/2026
0.4.0 303 7/21/2026
0.3.0-preview.15 136 7/21/2026
0.3.0-preview.14 135 7/21/2026
0.3.0-preview.13 127 7/21/2026
0.3.0-preview.12 128 7/21/2026
0.3.0-preview.11 134 7/21/2026
0.3.0-preview.10 119 7/21/2026
0.3.0-preview.9 127 7/21/2026
0.3.0-preview.8 131 7/21/2026
0.3.0-preview.7 125 7/21/2026
0.3.0-preview.6 113 7/20/2026
Loading failed

0.5.0-preview.1 — new error model: abstract Error catalog, AppResult for handlers, one Envelope response for every transport (ADR-026). Breaking for 0.4 consumers; migration guide: https://github.com/arigatory/Koto/blob/main/docs/migrations/0.4-to-0.5.md