Giter Site home page Giter Site logo

heleonix.guard's Introduction

Heleonix.Guard

Release: .NET / NuGet

Provides performant guard functionality for methods to throw exceptions

Install

https://www.nuget.org/packages/Heleonix.Guard

Documentation

See Heleonix.Guard

Example

using Heleonix.Guard;
using Heleonix.Extensions;
using static Heleonix.Guard.Host;

public static class Test
{
    public static void Main()
    {
        MyMethod(null);
    }

    public static void MyMethod(string param)
    {
        // C# 7.2+: Non-Trailing named arguments
        Throw.ArgumentNullException(when: param.IsNull(), nameof(param));

        // OR

        // Prior to C# 7.2: You can use a helper method 'When'
        Throw.ArgumentNullException(When(param.IsNull()), nameof(param));

        // OR
        Throw.ArgumentNullException(param == null, nameof(param));

        // OR
        Throw.ArgumentNullException(When(param == null), nameof(param));
    }
}

The Heleonix.Extensions provides many useful predicative extensions to build assertions, like IsNull(), IsNullOrEmptyOrWhiteSpace(), IsLessThan() etc. These extensions do not throw exceptions, so they will not overlap an exception to be really thrown. See Heleonix.Extensions for more details.

The Host.Throw property returns a singleton instance of the Heleonix.Guard.ExceptionRaiser class, which provides methods to throw many existing exceptions, like ArgumentNullException, FileLoadException etc. See more via VisualStudio intellisense.

The ExceptionRaiser returned by the Host.Throw is the singleton instance, so there is no creations of new objects every time, as it is usually implemented in fluent interfaces. Besides, it also provides ability to implement custom exception raisers via extension methods:

using Heleonix.Guard;

public static class ExceptionRaiserExtensions
{
#pragma warning disable CC0057 // Unused parameters
    public static void MyCustomException(this ExceptionRaiser raiser, bool when, int param1, string param2, object paramN, string message = null, Exception innerException = null)
    {
        if (when)
        {
            throw new MyCustomException(param1, param2, paramN, message, innerException);
        }
    }
#pragma warning restore CC0057 // Unused parameters
}

and then use it, for example, as below:

Throw.MyCustomException(when: param1.IsZero() || param2.IsNullOrEmpty() || paramN.IsNull(), "some message");

Contribution Guideline

  1. Create a fork from the main repository

  2. Implement whatever is needed

  3. Create a Pull Request. Make sure the assigned Checks pass successfully.

  4. Request review from the code owner

  5. Once approved, merge your Pull Request via Squash and merge

    IMPORTANT
    While merging, enter a Conventional Commits commit message. This commit message will be used in automatically generated Github Release Notes and NuGet Release Notes

  6. Monitor the Release: .NET / NuGet GitHub workflow to make sure your changes are delivered successfully

  7. In case of any issues, please contact [email protected]

heleonix.guard's People

Contributors

hennadiilu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.