Ardalis.SmartEnum.GuardClauses 8.1.0

dotnet add package Ardalis.SmartEnum.GuardClauses --version 8.1.0                
NuGet\Install-Package Ardalis.SmartEnum.GuardClauses -Version 8.1.0                
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="Ardalis.SmartEnum.GuardClauses" Version="8.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ardalis.SmartEnum.GuardClauses --version 8.1.0                
#r "nuget: Ardalis.SmartEnum.GuardClauses, 8.1.0"                
#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.
// Install Ardalis.SmartEnum.GuardClauses as a Cake Addin
#addin nuget:?package=Ardalis.SmartEnum.GuardClauses&version=8.1.0

// Install Ardalis.SmartEnum.GuardClauses as a Cake Tool
#tool nuget:?package=Ardalis.SmartEnum.GuardClauses&version=8.1.0                

Ardalis.SmartEnum.GuardClauses

Ardalis.SmartEnum.GuardClauses is a NuGet package that provides guard clauses to ensure input values are valid instances of a specified SmartEnum. It helps you to validate that a given value corresponds to a valid SmartEnum value and throws appropriate exceptions if it is not.

Installation

To install the Ardalis.SmartEnum.GuardClauses package, run the following command in the NuGet Package Manager Console:

Install-Package Ardalis.SmartEnum.GuardClauses

Alternatively, you can install it via the .NET CLI:

dotnet add package Ardalis.SmartEnum.GuardClauses

Usage

SmartEnumOutOfRange Method

The primary method provided by this package is SmartEnumOutOfRange, which can be used to validate if an input value is a valid SmartEnum.

Example Usage

Here's an example of how to use the SmartEnumOutOfRange method:

using Ardalis.GuardClauses;
using Ardalis.SmartEnum;
using Ardalis.SmartEnum.GuardClauses;
using System;

public class Status : SmartEnum<Status>
{
    public static readonly Status Draft = new Status(nameof(Draft), 1);
    public static readonly Status Published = new Status(nameof(Published), 2);
    public static readonly Status Archived = new Status(nameof(Archived), 3);

    private Status(string name, int value) : base(name, value) { }
}

public class Example
{
    public void ValidateStatus(int statusValue)
    {
        // This will throw a SmartEnumNotFoundException if the statusValue is not a valid Status
        Status status = Guard.Against.SmartEnumOutOfRange<Status>(statusValue);
        Console.WriteLine($"Validated status: {status.Name}");
    }
}

In this example, the ValidateStatus method checks if the statusValue is a valid Status SmartEnum. If the value is invalid, a SmartEnumNotFoundException is thrown.

Custom Exception Handling

You can also pass a custom exception creator function to the SmartEnumOutOfRange method:

public void ValidateStatusWithCustomException(int statusValue)
{
    try
    {
        Status status = Guard.Against.SmartEnumOutOfRange<Status>(statusValue, exceptionCreator: () => new ArgumentException("Invalid status value provided."));
        Console.WriteLine($"Validated status: {status.Name}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception caught: {ex.Message}");
    }
}

In this example, if the statusValue is not valid, a custom ArgumentException will be thrown instead of the default SmartEnumNotFoundException.

Supporting Different Value Types

The package also supports SmartEnums with different value types, such as string, Guid, etc.:

public class Color : SmartEnum<Color, string>
{
    public static readonly Color Red = new Color(nameof(Red), "FF0000");
    public static readonly Color Green = new Color(nameof(Green), "00FF00");
    public static readonly Color Blue = new Color(nameof(Blue), "0000FF");

    private Color(string name, string value) : base(name, value) { }
}

public void ValidateColor(string colorValue)
{
    Color color = Guard.Against.SmartEnumOutOfRange<Color, string>(colorValue);
    Console.WriteLine($"Validated color: {color.Name}");
}

In this case, the SmartEnumOutOfRange method checks if colorValue corresponds to a valid Color SmartEnum.

Additional Information

For more details on the SmartEnum package and its usage, check out the official SmartEnum repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please see the CONTRIBUTING guide for details.

Acknowledgements

Special thanks to Ardalis for creating the SmartEnum package and to all contributors for their ongoing efforts.


This README provides an overview of how to use the Ardalis.SmartEnum.GuardClauses package. Make sure to check out the SmartEnum repository for further examples and documentation.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
8.1.0 165 10/3/2024
8.0.0 81 10/3/2024

See root package release notes