Pragmatic.Ensure 1.0.0-alpha.6

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

Pragmatic.Ensure

Guard clauses for parameter validation in .NET: consistent, fast, and intention-revealing.

The Problem

Every codebase validates inputs, and without a shared approach guard clauses become a patchwork: inconsistent exception types, forgotten guards, and exceptions thrown for business cases that should return typed errors.

_repository = repository ?? throw new ArgumentNullException(nameof(repository));   // dev A
_logger = logger;                                                                  // dev B forgot
if (id == Guid.Empty) throw new InvalidOperationException("Id cannot be empty");   // dev C, wrong type
if (order is null) throw new KeyNotFoundException($"Order {id} not found");        // dev D, business case

The Solution

One consistent, readable guard API that throws the right exception with the right message, and a clear boundary: guards are for programmer errors (preconditions), not business outcomes (use Result for those).

using static Pragmatic.Ensure.Ensure;

public OrderService(IOrderRepository repository, ILogger<OrderService> logger)
{
    ThrowIfNull(repository);
    ThrowIfNull(logger);
    // ...
}

public async Task<Order> GetOrderAsync(Guid id)
{
    ThrowIfEmpty(id);                          // ArgumentException with the right message
    return await _repository.GetByIdAsync(id);
}

Argument names are captured automatically via [CallerArgumentExpression], no nameof noise. The guards are aggressively inlined and allocation-free on the success path.

Installation

dotnet add package Pragmatic.Ensure

Status

Stable within 1.0.0-alpha: the guard surface is settled. See the roadmap.

Documentation

Guide What you'll learn
Concepts Guards vs Result, the precondition boundary, design
Getting Started Your first guards
API Reference Every ThrowIf* / Is* / Check*, parameters, null-safety contract
Best Practices When to guard vs return a typed error
Common Mistakes The most frequent guard pitfalls
Troubleshooting Problem/solution guide

Requirements

  • .NET 10.0+

License

Part of the Pragmatic.Design ecosystem. See Licensing. Pragmatic.Ensure is MIT-licensed.

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

    • No dependencies.
  • net11.0

    • No dependencies.

NuGet packages (19)

Showing the top 5 NuGet packages that depend on Pragmatic.Ensure:

Package Downloads
Pragmatic.Internationalization

Unified internationalization library combining globalization (formatting) and localization (translations). Money type, currency codes, plural strings, relative time, and type-safe translations.

Pragmatic.Events

Domain events infrastructure: IDomainEvent, handlers, dispatchers. Pure domain package, zero infrastructure dependencies.

Pragmatic.Mapping

Source-generated object mapping with zero reflection. Compile-time validated, zero allocation hot paths.

Pragmatic.Specification

Specification pattern for composable, reusable predicates. Works with IQueryable and IEnumerable.

Pragmatic.Persistence

Persistence abstractions for Pragmatic.Design - Entity definitions, Repository pattern, UnitOfWork, and DTO-driven Query/Mutation patterns. Database-agnostic, works with any ORM.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.6 638 11/21/2026
1.0.0-alpha.5 574 11/21/2026
1.0.0-alpha.4 618 11/16/2026
1.0.0-alpha.3 875 10/17/2026
1.0.0-alpha.2 479 10/12/2026
1.0.0-alpha.1 701 10/3/2026