LawBook 1.0.1
See the version list below for details.
dotnet add package LawBook --version 1.0.1
NuGet\Install-Package LawBook -Version 1.0.1
<PackageReference Include="LawBook" Version="1.0.1" />
paket add LawBook --version 1.0.1
#r "nuget: LawBook, 1.0.1"
// Install LawBook as a Cake Addin #addin nuget:?package=LawBook&version=1.0.1 // Install LawBook as a Cake Tool #tool nuget:?package=LawBook&version=1.0.1
LawBook
LawBook provide some classes that helps you to do validations on your own classes. You can validate a lot of things, if values match or not, is on a specified interval, is contained on a list of values, match to a regex expression and a lot of other things.
This project was made because in a lot of projects I had to reproduce this validations structures, so I came with the idea to create a package with this solution and share with people who might find this helpfull.
The name of the solution, classes and methods came with an analogy to the law principles, where is specified your rights, rules and obligations to live in a society. So, in a software conception, the objects must follow too the rights, rules and obligations to make a software as expected.
How to use
You can run the validations in two ways:
- Calling in code explicitly each of the validations;
- Encapsule all the validations in a method and call by an Executor.
The first way is calling it explicitly, just call the method of the validation needed whever you want:
Law.MustBeTrue(1 > 2, "1 is not higher than 2!");
So, when you call this expression is going to throw an GuiltyException, because the validation failed.
And the other is encapsuling the validations in a method and call than with an executor, this way the validations are going to be centralized and you can call at the moment you want and multiple times.
public class Car : ILawable
{
public string FuelType { get; set; }
public int Wheels { get; set; }
public int Doors { get; set; }
public Car(string fuel, int wheels, int doors)
{
FuelType = fuel;
Wheels = wheels;
Doors = doors;
}
public void Validate()
{
Law.MustBeContained(FuelType, new string[] { "Gasoline", "Biodiesel" }, "Invalid Fuel Type");
Law.MustBeEquals(Wheels, 4, "Invalid Number of Wheels");
Law.MustBeContained(Doors, new int[] { 2, 4}, "Invalid Number of Doors");
}
}
For example, this class Car, was created the method Validate, inherited by the interface ILawable flaging that this class is validatable. And on the method you can see all the validations that an object of the type Car must follow.
And after to trigger the validations you can do this by two ways:
The simple one, like all methods are called:
myCar.Validate();
And using an executor:
Judge.Sentence(myCar, false);
But, why using an executor to run the method? You can do this just calling the Validate()
.
This is because, with the Executor we can pass configurations and manage how the validations are going to be executed.
This project is young, and the executor currently only have a flag to silence or not the thrown of the exception. Have a lot more planned to grow and make this more suitable and strong solution.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.