AlinSpace.Arguments 6.0.10

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
.NET 6.0
dotnet add package AlinSpace.Arguments --version 6.0.10
NuGet\Install-Package AlinSpace.Arguments -Version 6.0.10
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="AlinSpace.Arguments" Version="6.0.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AlinSpace.Arguments --version 6.0.10
#r "nuget: AlinSpace.Arguments, 6.0.10"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install AlinSpace.Arguments as a Cake Addin
#addin nuget:?package=AlinSpace.Arguments&version=6.0.10

// Install AlinSpace.Arguments as a Cake Tool
#tool nuget:?package=AlinSpace.Arguments&version=6.0.10

AlinSpace.Arguments

NuGet version (AlinSpace.Arguments)

A simple fluent library for function argument validation.

NuGet package

Why?

If you simply want to make sure that an argument can't be null, then the code might look like this:

// Check if the argument is not null.
if(argument == null)
    throw new ArgumentNullException(nameof(argument));

The problem with this is, that it does not scale well when the validation logic gets more complex. Here is a more complex argument validation:

// Check if the argument is not null.
if(argument == null)
    throw new ArgumentNullException(nameof(argument));
    
// And check if the string is at least 5 characters long.
if (argument.Length < 5)
    throw new ArgumentException(nameof(argument), $"String can't be shorter than 5 characters.");

With the AlinSpace.Arguments library the code could be rewritten to this:

Argument
    .Wrap(argument, nameof(argument))
    .IsNotNull()
    .Is(s => s.Length >= 5, message: $"String can't be shorter than 5 characters.");

It is much more compact, easier to read and understand, flexible, and more consistent. Custom validation constraints can be added in form of extension methods or as a predicate functions. It is also possible to retrieve the checked argument after validation, like this:

string checkedArgument = Argument
    .Wrap(uncheckedArgument, nameof(uncheckedArgument))
    .IsNotNull()
    .Is(s => s.Length >= 5);

Examples

To validate an argument with this library, the argument first has to be wrapped by an argument wrapper. On this wrapper validation methods can be performed.

The following code snippet checks the string argument for not-null:

// Check if the argument is not null.
string checkedArgument = Argument
    .Wrap(uncheckedArgument)
    .IsNotNull();

The fluent design allows the validation methods to be chained together easily.

// Check if the argument is not null, contains at least one 'a' character
// and is at least 5 characters long.
string checkedArgument = Argument
    .Wrap(uncheckedArgument)
    .IsNotNull()
    .Is(s => s.Contains("a"))
    .IsNot(s => s.Length >= 5);

Custom validation rules

There are two ways to add custom validation rules.

1 Predicate Function

The trivial way is to simply pass a predicate function:

bool MyPredicateFunction<TArgument>(TArgument argument)
{
    // Validate the given argument, return true on successful validation; false otherwise.
}

Argument
    .Wrap(uncheckedArgument)
    .Is(MyPredicateFunction);

2 Extension method

If you have an argument validation rule that you would like to define in a class and use throughout your codebase, than custom extension methods might be a better way of doing it:

public static class MyArgumentWrapperExtensions
{
    public static ArgumentWrapper<TArgument> MyCustomRule(ArgumentWrapper<TArgument> argument)
    {
        // Validate the given argument here and throw an exception when the rule is violated.
	
	return argument;
    }
}

Argument
    .Wrap(uncheckedArgument)
    .MyCustomRule();
Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.10 96 11/1/2022
6.0.9 133 10/12/2022
6.0.8 134 10/11/2022
6.0.5 203 2/9/2022
6.0.0 609 11/19/2021
5.0.1 296 8/1/2021
5.0.0 213 7/30/2021