Skip to content

Contracts

Arsenty Politov edited this page Mar 15, 2019 · 1 revision

Contracts library

Provide classes for validating various conditions and values that are originated from arguments or state.

Related Packages

  • DevGuild.AspNetCore.Contracts

Description

This package allows performing less cumbersome checks of the various values when failure should be followed by a thrown exception. Instead of performing manual checks and throwing exception, this package allows to perform the check with a single method call that will throw an exception if the check fails.

Two origins of the value are currently supported: a method argument and a state.

When using argument-related checks, the type of exception thrown is either ArgumentException or ArgumentNullException with the argument name included.

State-related checks are more generic and should probably be suitable for most non-argument checks. When such check is failed the InvalidOperationException is thrown.

All methods used for performing the checks have aggressive inlining turned on so there should be no performance impact in comparison with manual if-throw check.

Example usage

Validating arguments manually:

public Permission(Guid id, [NotNull]String name, Int32 bits)
{
    if (name == null)
    {
        throw new ArgumentNullException(nameof(name));
    }

    if (bits <= 0)
    {
        throw new ArgumentException("name must be greater than 0");
    }

    this.Id = id;
    this.Name = name;
    this.Bits = bits;
}

Validating arguments using this package:

public Permission(Guid id, [NotNull]String name, Int32 bits)
{
    Ensure.Argument.NotNull(name, nameof(name));
    Ensure.Argument.GreaterThan(bits, 0, nameof(bits));

    this.Id = id;
    this.Name = name;
    this.Bits = bits;
}

Validating state manually:

public ImageUploadConfiguration BuildConfiguration()
{
    if (String.IsNullOrEmpty(this.containerName) || this.containerPrefix == null)
    {
        throw new InvalidOperationException("Container is not configured");
    }

    if (this.allowedFormats.Count == 0)
    {
        throw new InvalidOperationException("No image format was allowed");
    }

    if (this.variations.Count == 0)
    {
        throw new InvalidOperationException("No variation was defined");
    }

    return new ImageUploadConfiguration(this.identifier, this.version, this.containerName, this.containerPrefix, this.allowedFormats, this.variations);
}

Validating state using this package:

public ImageUploadConfiguration BuildConfiguration()
{
    Ensure.State.NotNullOrEmpty(this.containerName, "Container is not configured");
    Ensure.State.NotNull(this.containerPrefix, "Container is not configured");
    Ensure.State.HasElements(this.allowedFormats, "No image format was allowed");
    Ensure.State.HasElements(this.variations, "No variation was defined");

    return new ImageUploadConfiguration(this.identifier, this.version, this.containerName, this.containerPrefix, this.allowedFormats, this.variations);
}

Supported validations

Name Description Argument State
NotNull Checks if value is not null + +
Null Checks if value is null + +
HasValue Checks if nullable value has a value + +
DoesNotHaveValue Checks if nullable value does not have a value + +
MeetCondition Checks if a custom condition is met + +
DoesNotMeetCondition Checks if a custom condition is not met + +
EqualTo Checks if value is equal to specific value + +
NotEqualTo Checks if value is not equal to specific value + +
GreaterThan Checks if value is greater than specific value + +
GreaterThanOrEqualTo Checks if value is greater than or equal to specific value + +
LessThan Checks if value is less than specific value + +
LessThanOrEqualTo Checks if value is less than or equal to specific value + +
NotEmpty Checks if string value is not equal to empty string + +
Empty Checks if string value is equal to empty string + +
NotNullOrEmpty Checks if string value is not null or equal to empty string + +
NullOrEmpty Checks if string value is null or equal to empty string + +
HasNoElements Checks if collection value is empty + +
HasElements Checks if collection value is not empty + +
HasSingleElement Checks if collection value has a single element + +
HasExactNumberOfElements Checks if collection value has a specific number of elements + +
IsConvertible.ToGuid Checks if value could be converted to Guid type +

Clone this wiki locally