Skip to content

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

Notifications You must be signed in to change notification settings

george-pancescu/Guard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)));

    ...
	
}

Installation

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

Install-Package Guard.NET

The package has no external dependencies.

About

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

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages