Guard.NET 1.2.0

A simple library that facilitates runtime checks of code and allows to define preconditions and invariants within a method.

Install-Package Guard.NET -Version 1.2.0
dotnet add package Guard.NET --version 1.2.0
<PackageReference Include="Guard.NET" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Guard.NET --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Guard.NET

A simple library that facilitates runtime checks of code and allows to define preconditions and invariants within a method.

Its main purpose is to leverage the precondition checks that appear in almost all methods, through a clean interface that accentuates intention and eliminates confusion.

Usage

public void AddUser(User user)
{
    Guard.NotNull(user, nameof(user), "optional custom error message");  // throws ArgumentNullException
	
    // OR use an explicit Exception
    
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotNull(user, invalidOperationException); 
    
    ...

}
public void GetUserByName(string name)
{
    Guard.NotNullOrWhitespace(name, nameof(name), "optional custom error message"); // throws ArgumentException

    // OR use an explicit Exception
    
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotNullOrWhitespace(userName, invalidOperationException);
    
    ...
	
}
public void GetUsers(int pageSize)
{
    Guard.NotGreaterThan(pageSize, _maxPageSize, nameof(pageSize), "optional custom error message"); // throws ArgumentOutOfRangeException

    // OR use an explicit Exception
    
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotGreaterThan(pageSize, _maxPageSize, invalidOperationException);
    
    ...
	
}
public void UpdateEmailAddress(int userId, string newEmailAddress)
{
    Guard.For(() => userId < 0, new ArgumentException(nameof(userId)));
    Guard.For(() => Regex.IsMatch(newEmailAddress, _emailRegexPattern), new ArgumentException(nameof(newEmailAddress)));

    ...
	
}

Instalation

The Guard class can be used by installing Guard.NET nuget package available here.

Install-Package Guard.NET

The package has no external dependencies.

Guard.NET

A simple library that facilitates runtime checks of code and allows to define preconditions and invariants within a method.

Its main purpose is to leverage the precondition checks that appear in almost all methods, through a clean interface that accentuates intention and eliminates confusion.

Usage

public void AddUser(User user)
{
    Guard.NotNull(user, nameof(user), "optional custom error message");  // throws ArgumentNullException
	
    // OR use an explicit Exception
    
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotNull(user, invalidOperationException); 
    
    ...

}
public void GetUserByName(string name)
{
    Guard.NotNullOrWhitespace(name, nameof(name), "optional custom error message"); // throws ArgumentException

    // OR use an explicit Exception
    
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotNullOrWhitespace(userName, invalidOperationException);
    
    ...
	
}
public void GetUsers(int pageSize)
{
    Guard.NotGreaterThan(pageSize, _maxPageSize, nameof(pageSize), "optional custom error message"); // throws ArgumentOutOfRangeException

    // OR use an explicit Exception
    
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotGreaterThan(pageSize, _maxPageSize, invalidOperationException);
    
    ...
	
}
public void UpdateEmailAddress(int userId, string newEmailAddress)
{
    Guard.For(() => userId < 0, new ArgumentException(nameof(userId)));
    Guard.For(() => Regex.IsMatch(newEmailAddress, _emailRegexPattern), new ArgumentException(nameof(newEmailAddress)));

    ...
	
}

Instalation

The Guard class can be used by installing Guard.NET nuget package available here.

Install-Package Guard.NET

The package has no external dependencies.

Release Notes

Added new Guards (NotIsMatch, NotEqualTom, NotAny, NotLessThanOrEqualTo, NotGreaterThanOrEqualTo).
Added new overloads for all Guards, allowing to specify a custom Exception as a generic parameter (without passing any concrete instance).

  • .NETCoreApp 2.1

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 1.3

  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.2.0 14,249 8/24/2018
1.0.1 141 8/23/2018
1.0.0.2 841 6/25/2018
1.0.0.1 221 6/19/2018
1.0.0 244 4/10/2018