A simple package with extension methods to forbid some value matches,logical values or ranges in .Net
If you like or are using this project please give it a star. Thanks!
Using dotnet cli
dotnet add package Forbid --version 1.0.5
or package reference
<PackageReference Include="Forbid" Version="1.0.5" />
- 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.
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
}
}
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.