https://github.com/revazashvili/forbid
A simple package with extension methods to forbid some value matches,logical values or ranges in .Net
https://github.com/revazashvili/forbid
clause empty exception extension forbid null
Last synced: about 1 year ago
JSON representation
A simple package with extension methods to forbid some value matches,logical values or ranges in .Net
- Host: GitHub
- URL: https://github.com/revazashvili/forbid
- Owner: Revazashvili
- License: mit
- Created: 2021-09-13T16:36:04.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-04-16T06:07:08.000Z (about 3 years ago)
- Last Synced: 2025-02-15T12:41:29.925Z (over 1 year ago)
- Topics: clause, empty, exception, extension, forbid, null
- Language: C#
- Homepage:
- Size: 133 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Forbid
A simple package with extension methods to forbid some value matches,logical values or ranges in .Net
[](https://dotnet.microsoft.com/)

[](https://www.nuget.org/packages/Forbid)
[](https://github.com/Revazashvili/Forbid/blob/main/LICENSE)
## Give a Star! :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
```
```
## 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
```c#
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
```c#
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](https://www.nuget.org/packages/Forbid.Extensions.Microsoft.DependencyInjection/).
```c#
using Forbids;
public void ConfigureServices(IServiceCollection services)
{
services.AddForbids(); //default is Singleton, you can pass ServiceLifetime
}
```
and then
```c#
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:
```c#
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.