EnsureFramework 1.3.2

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

// Install EnsureFramework as a Cake Tool
#tool nuget:?package=EnsureFramework&version=1.3.2                

EnsureFramework

EnsureFramework is designed to take the pain out of null checking and making sure (ensuring) your methods are being used the way they were intended and throwing exceptions when unsupported values are passed to them.

You can make sure values are in a specific range or simply make sure that an argument is not null.

It was designed to be readable and fluent based, meaning you can chain ensurables together and the first one that fails will result in the exception.

101

  1. Add the namespace
    namespace EnsureFramework;
    
  2. Ensure things!
    argument-string method
    public void MyMethod(string anArgument)
    {
        Ensure.Arg(anArgument, nameof(anArgument)).IsNotNullOrEmpty();
    }
    
    ... or you can use expressions...
    expression based method
    public void MyMethod(string anArgument)
    {
        Ensure.Arg(() => anArgument).IsNotNullOrEmpty();
    }
    

Other things

You can ensure your arguments directly using the argument-string override or with the expression override. You need to be aware that if performance is desired the argument-string override is the best.

Supported types

There are extensions for common types in .NET but not much else outside of that. We support:

  • IComparable ⫽ for example...
    • System.Decimal
    • System.Enum
    • System.String
    • System.DateTime
    • System.Boolean
    • System.Byte
    • System.Char
    • System.DateTimeOffset
    • System.Double
    • System.Guid
    • System.Int16
    • System.Int32
    • System.Int64
    • System.SByte
    • System.Single
    • System.TimeSpan
    • System.Tuple<T1>
    • System.Tuple<T1,T2>
    • System.Tuple<T1,T2,T3>
    • System.Tuple<T1,T2,T3,T4>
    • System.Tuple<T1,T2,T3,T4,T5>
    • System.Tuple<T1,T2,T3,T4,T5,T6>
    • System.Tuple<T1,T2,T3,T4,T5,T6,T7>
    • System.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>
    • System.UInt16
    • System.UInt32
    • System.UInt64
    • System.ValueTuple
    • System.ValueTuple<T1>
    • System.ValueTuple<T1,T2>
    • System.ValueTuple<T1,T2,T3>
    • System.ValueTuple<T1,T2,T3,T4>
    • System.ValueTuple<T1,T2,T3,T4,T5>
    • System.ValueTuple<T1,T2,T3,T4,T5,T6>
    • System.ValueTuple<T1,T2,T3,T4,T5,T6,T7>
    • System.ValueTuple<T1,T2,T3,T4,T5,T6,T7,TRest>
    • System.Version
  • IEnumerable
    • List
    • Collection
    • Dictionary
  • Dictionary
  • Guid
  • Int32/int
  • Object
  • String

Well Mike, my type isn't supported...

That's cool friend, just create your own by adding extension methods to your project. Here is a simple one that should point you in the right direction:

public static IArgumentAssertionBuilder<string> IsNotNullOrEmpty(this IArgumentAssertionBuilder<string> @this)
{
    if (@this.Argument == null)
    {
        throw new ArgumentNullException(@this.ArgumentName);
    }
    if (@this.Argument == string.Empty)
    {
        throw new ArgumentException(null, @this.ArgumentName);
    }
    return @this;
}

Check out the code and tests (or lack-there-of) for a bit more info - the project is quite simple.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 netcoreapp1.0 was computed.  netcoreapp1.1 is compatible.  netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.6 is compatible.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  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 tizen30 was computed.  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.
  • .NETCoreApp 1.1

  • .NETCoreApp 2.0

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETStandard 1.6

  • .NETStandard 2.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
1.3.2 790 10/22/2019
1.3.1 693 7/23/2019
1.3.0 685 5/27/2019
1.2.1 1,263 7/4/2018
1.2.0 1,092 6/15/2018
1.1.0 1,085 4/16/2018
1.0.0 1,411 8/30/2016