Shuttle.Core.Contract 21.0.1-beta

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

Shuttle.Core.Contract

A guard implementation that performs assertions/assumptions to prevent invalid code execution.

Installation

dotnet add package Shuttle.Core.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 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

Throws exception TException with the given message if the assertion is false. If exception type TException does not have a constructor that accepts a message then an InvalidOperationException is thrown instead.


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 and 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 an ArgumentException if the given 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 InvalidOperationException if the provided value cannot be found in the given TEnum; else returns the value as TEnum.

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 (41)

Showing the top 5 NuGet packages that depend on Shuttle.Core.Contract:

Package Downloads
Shuttle.Esb

Contains the core Shuttle.Esb assembly that should always be referenced when building Shuttle.Esb solutions.

Shuttle.Core.Reflection

Reflection infrastructure components.

Shuttle.Core.Logging

Logging adapter.

Shuttle.Core.Specification

Simple specification mechanism.

Shuttle.Core.Threading

Thread-based processing.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
21.0.1-beta 254 2/7/2026
21.0.0-alpha 244 1/9/2026
20.0.1 3,143 5/6/2025
20.0.0 18,431 2/2/2025
11.1.1 2,755 7/21/2024
11.1.0 8,222 4/30/2024
11.0.0 30,138 12/1/2022
10.1.0 42,429 5/27/2022
10.0.4 1,162 5/21/2022
10.0.3 106,237 11/26/2020
10.0.2 34,404 2/13/2020
10.0.1 158,193 7/3/2018
10.0.0 98,185 12/27/2017