Shuttle.Contract
21.0.1-rc3
Prefix Reserved
dotnet add package Shuttle.Contract --version 21.0.1-rc3
NuGet\Install-Package Shuttle.Contract -Version 21.0.1-rc3
<PackageReference Include="Shuttle.Contract" Version="21.0.1-rc3" />
<PackageVersion Include="Shuttle.Contract" Version="21.0.1-rc3" />
<PackageReference Include="Shuttle.Contract" />
paket add Shuttle.Contract --version 21.0.1-rc3
#r "nuget: Shuttle.Contract, 21.0.1-rc3"
#:package Shuttle.Contract@21.0.1-rc3
#addin nuget:?package=Shuttle.Contract&version=21.0.1-rc3&prerelease
#tool nuget:?package=Shuttle.Contract&version=21.0.1-rc3&prerelease
Shuttle.Contract
A guard implementation that performs assertions/assumptions to prevent invalid code execution.
Installation
dotnet add package Shuttle.Contract
Guard
void Against<TException>(bool condition, string message) where TException : Exception
Throws exception TException with the given message if the condition is true. If exception type TException does not have a constructor that accepts a message then an InvalidOperationException is thrown instead.
Guard.Against<ArgumentException>(age < 0, "Age cannot be negative");
string AgainstEmpty(string? value, [CallerArgumentExpression("value")] string? name = null)
Throws a ArgumentNullException if the given value is null or empty/whitespace; else returns the value.
Guid AgainstEmpty(Guid value, [CallerArgumentExpression("value")] string? name = null)
Throws an ArgumentException when the value is equal to an empty Guid ({00000000-0000-0000-0000-000000000000}); else returns the value.
IEnumerable<T> AgainstEmpty<T>(IEnumerable<T> enumerable, [CallerArgumentExpression("enumerable")] string? name = null)
Throws a ArgumentNullException if the given enumerable is null, or an ArgumentException if the enumerable does not contain any entries; else returns the enumerable.
T AgainstNull<T>(T? value, [CallerArgumentExpression("value")] string? name = null)
Throws a ArgumentNullException if the given value is null; else returns the value.
TEnum AgainstUndefinedEnum<TEnum>(object? value, [CallerArgumentExpression("value")] string? name = null)
Throws an ArgumentNullException if the given value is null, or an InvalidOperationException if the provided value cannot be found in the given TEnum; else returns the value as TEnum. Accepts both enum values and string representations.
Usage Examples
// Basic null checking
var name = Guard.AgainstNull(userName); // Throws ArgumentNullException if userName is null
var email = Guard.AgainstNull(userEmail, nameof(userEmail)); // Exception message includes parameter name
// String validation
var password = Guard.AgainstEmpty(userPassword); // Throws ArgumentNullException if null, empty, or whitespace
var title = Guard.AgainstEmpty(documentTitle, nameof(documentTitle)); // Includes parameter name in exception
// Collection validation
var items = Guard.AgainstEmpty(orderItems, nameof(orderItems)); // Throws if null or empty
// Custom assertions
Guard.Against<ArgumentException>(userId > 0, "User ID must be positive"); // Throws ArgumentException if condition is false
// Enum validation
var status = Guard.AgainstUndefinedEnum<OrderStatus>("Pending", nameof(status)); // Converts string to enum or throws
| 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. |
-
net10.0
- No dependencies.
NuGet packages (21)
Showing the top 5 NuGet packages that depend on Shuttle.Contract:
| Package | Downloads |
|---|---|
|
Shuttle.Recall
Event sourcing mechanism. |
|
|
Shuttle.Access
An abstraction for simple identity and access control. |
|
|
Shuttle.Access.Messages
Messages for use in Shuttle.Access implementations. |
|
|
Shuttle.Access.RestClient
A client used to interact with the restful Shuttle.Access web API. |
|
|
Shuttle.Access.AspNetCore
Authorization middleware for web API endpoints using Shuttle.Access. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 21.0.1-rc3 | 86 | 4/11/2026 |