Pragmatic.Ensure
1.0.0-alpha.6
dotnet add package Pragmatic.Ensure --version 1.0.0-alpha.6
NuGet\Install-Package Pragmatic.Ensure -Version 1.0.0-alpha.6
<PackageReference Include="Pragmatic.Ensure" Version="1.0.0-alpha.6" />
<PackageVersion Include="Pragmatic.Ensure" Version="1.0.0-alpha.6" />
<PackageReference Include="Pragmatic.Ensure" />
paket add Pragmatic.Ensure --version 1.0.0-alpha.6
#r "nuget: Pragmatic.Ensure, 1.0.0-alpha.6"
#:package Pragmatic.Ensure@1.0.0-alpha.6
#addin nuget:?package=Pragmatic.Ensure&version=1.0.0-alpha.6&prerelease
#tool nuget:?package=Pragmatic.Ensure&version=1.0.0-alpha.6&prerelease
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 | Versions 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. |
-
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 |