NoSuchCompany.Diagnostics.Guards 1.0.51

There is a newer version of this package available.
See the version list below for details.
dotnet add package NoSuchCompany.Diagnostics.Guards --version 1.0.51
NuGet\Install-Package NoSuchCompany.Diagnostics.Guards -Version 1.0.51
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="NoSuchCompany.Diagnostics.Guards" Version="1.0.51" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NoSuchCompany.Diagnostics.Guards --version 1.0.51
#r "nuget: NoSuchCompany.Diagnostics.Guards, 1.0.51"
#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 NoSuchCompany.Diagnostics.Guards as a Cake Addin
#addin nuget:?package=NoSuchCompany.Diagnostics.Guards&version=1.0.51

// Install NoSuchCompany.Diagnostics.Guards as a Cake Tool
#tool nuget:?package=NoSuchCompany.Diagnostics.Guards&version=1.0.51

NoSuchCompany.Diagnostics.Guards

Build status

The Diagnostics library provides simple and light classes that help checking incoming parameters of a method using the Fluent API or not, depending on what you prefer.

The library is provided as a .netstandard2.0 DLL so that it can be used in .NET Framework projects (4.6.1) as well as in .NET Core applications. The choice is yours.

Getting Started

  1. Install the standard Nuget package into your application.
Package Manager : Install-Package NoSuchCompany.Diagnostics.Guards
CLI : dotnet add package NoSuchCompany.Diagnostics.Guards
  1. Well.... use the methods of the Argument static class.
using NoSuchCompany.Diagnostics.Guards;

namespace Application
{
    public class SomeClass
    {
        public SomeClass(string name, Datetime timestampUtc, Socket someSocket)
        {
            Argument.ThrowIfIsEmpty(name, nameof(name));
            Argument.ThrowIfIsNotUtc(timestampUtc, nameof(timestampUtc));
            Argument.ThrowIfIsNull(someSocket, nameof(someSocket));
            
            //  ...            
        }
    }
}
  1. Or use the Fluent approach:
using NoSuchCompany.Diagnostics.Guards;

namespace Application
{
    public class SomeClass
    {
        public SomeClass(string name, Datetime timestampUtc, Socket someSocket)
        {
            name.ThrowIfIsEmpty(nameof(name));
            timestampUtc.ThrowIfIsNotUtc(nameof(timestampUtc));
            someSocket.ThrowIfIsNull(nameof(someSocket));
            
            //  ...            
        }
    }
}

Features

  1. Reference types

First, the API provides with a way to prevent null instances from creeping up your application. For example:

public void DoSomething(SomeReferenceType inst)
{
    Argument.ThrowIfIsNull(inst, nameof(inst));   // Stop if inst is bs.
}
  1. Guid

We often use Guid as identifiers of entities. We always expect them to represent some entity somewhere in the system. Knowing that we create new entity and assign them a Guid.NewGuid(), empty Guids are not expected and may be a sign of some bug. Hence the following check:

public void DoSomething(Guid entityId)
{
    Argument.ThrowIfIsEmpty(entityId, nameof(entityId));   // Stop if entityId is some bs instance.
}
  1. DateTime

A lot of our applications are used by customers across several time zones. As such, our backend does not manipulate DateTime instances that are specified in the local time zone, but only with those specified in the UTC time zone. We let the front-end deal with the time zone. To avoid tricky bugs, we have the following check whenever needed:

public void DoSomething(DateTime timestampUtc)
{
    Argument.ThrowIfIsNotLocal(timestampUtc, nameof(timestampUtc));   // Stop if timestampUtc is not a DateTime expressed in the UTC time zone.
}

The opposite can also be checked:

public void DoSomething(DateTime timestampLocal)
{
    Argument.ThrowIfIsNotUtc(timestampLocal nameof(timestampLocl));   // Stop if timestampLocal is not a DateTime expressed in a local time zone.
}
  1. Strings

In some cases, you may require string instances to have some value in it aside from an empty or white-spaced list of characters. Here is the check:

public void NotifyUser(string someImportantMsg)
{
    Argument.ThrowIfIsNullOrWhiteSpace(someImportantMsg nameof(someImportantMsg));   // Stop if we are not notifying anything to the user.
}
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 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.

This package has 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.0.71 3,120 3/27/2019
1.0.68 1,282 12/30/2018
1.0.65 1,717 8/21/2018
1.0.59 1,249 8/21/2018
1.0.51 1,272 8/20/2018
1.0.49 1,234 8/16/2018
1.0.41 1,348 8/14/2018