Giter Club home page Giter Club logo

forbid's Introduction

Forbid

A simple package with extension methods to forbid some value matches,logical values or ranges in .Net

.NET Nuget NuGet stable version GitHub license

Give a Star! โญ

If you like or are using this project please give it a star. Thanks!

Installing

Using dotnet cli

dotnet add package Forbid --version 1.0.5

or package reference

<PackageReference Include="Forbid" Version="1.0.5" />

Supported Methods

  • Null (throws if input is null.)
  • NullOrEmpty (throws if string,guid or IEnumerable is null or empty.)
  • NullOrWhitespace (throws if string input is null, empty or whitespace.)
  • Zero (throws if number input is zero.)
  • True (throws if boolean input is true.)
  • False (throws if boolean input is false.)
  • Range (throws if input is in provided range.)
  • Equal (throws if input is equal to provided value.)
  • NotEqual (throws if input is not equal to provided value.)
  • Positive (throws if input is positive number.)
  • PositiveOrZero (throws if input is positive number or zero.)
  • Negative (throws if input is negative number.)
  • NegativeOrZero (throws if input is negative number or zero.)
  • MoreThan (throws if input is more than provided value.)
  • MoreThanOrEqual (throws if input is more than or equal to provided value.)
  • LessThan (throws if input is less than provided value.)
  • LessThanOrEqual (throws if input is less than or equal to provided value.)
  • Any(throws if any element in input satisfies a condition.)
  • Count(throws if input count equals passed count.)
  • Length(throws if input string length equals passed length.)

You can pass message or custom exception to methods.

Usage

public class TestClass
{
    private readonly ITestService _testService;
    private readonly int _someValue;
        
    public TestClass(ITestService testService,int someValue)
    {
        _testService = Forbid.From.Null(_testService);
        _someValue = Forbid.From.NegativeOrZero(someValue);
    }
}

Or

public class TestClassWithCustomMessageOrException
{
    private readonly ITestService _testService;
    private readonly int _someValue;
        
    public TestClassWithCustomMessageOrException(ITestService testService,int someValue)
    {
        _testService = Forbid.From.Null(_testService,"test service must not be null.");
        _someValue = Forbid.From.NegativeOrZero(someValue,new CustomException());
    }
}

Or you can inject service in DI Container and use IForbid interface. for this install nuget package Forbid.Extensions.Microsoft.DependencyInjection.

using Forbids;

public void ConfigureServices(IServiceCollection services)
{
    services.AddForbids(); //default is Singleton, you can pass ServiceLifetime
}

and then

public class TestClassWithDISupport
{
    private readonly IForbid _forbid;
    
    public TestClassWithDISupport(IForbid forbid)
    {
        _forbid = forbid;
    }
    
    public void SomeForbid(int value)
    {
        _forbid.Zero(value);
        // more logic here
    }
}

Extend Forbids

To extend forbids, you can do the following:

public static class TestExtend
    {
        public static string Test(this IForbid forbid, string input,Exception exception)
        {
            if (input == "test")
                throw exception;
            return input;
        }
    }

or you can open a pull request and contribute to my project.

forbid's People

Contributors

revazashvili avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

shsahin

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.