DomainValidation.NET
1.1.0
dotnet add package DomainValidation.NET --version 1.1.0
NuGet\Install-Package DomainValidation.NET -Version 1.1.0
<PackageReference Include="DomainValidation.NET" Version="1.1.0" />
paket add DomainValidation.NET --version 1.1.0
#r "nuget: DomainValidation.NET, 1.1.0"
// Install DomainValidation.NET as a Cake Addin
#addin nuget:?package=DomainValidation.NET&version=1.1.0
// Install DomainValidation.NET as a Cake Tool
#tool nuget:?package=DomainValidation.NET&version=1.1.0
Domain validation with returning result object
This project is an approach to domain validation with a result object. The Result
class is used to handle errors and results in your application in a consistent and reliable way. If there is no validation issue and if the model is valid, then result.Value
will return the value which is valid model and the result.IsSuccess
is true. However, if something is wrong with domain validation, then result.IsSuccess
is false and result.Error
will hold error code and description.
Example:
Here we have a Blog
class with a domain validation logic:
public class Blog
{
private Blog(string title)
{
Title = title;
}
public static Result<Blog> Create(string title)
{
var errors = new List<Error>();
if (string.IsNullOrEmpty(title))
{
return Result.Failure<Blog>(new Error("Error.Title.Empty", "Title is mandatory"));
}
if (title.Length < 3)
{
errors.Add(new Error("Error.Title", "The minimum title length is 3 character"));
}
if (title.Length > 40)
{
errors.Add(new Error("Error.Title.MaxLength", "The maximum title length is 40 character"));
}
if (errors.Any())
{
return Result.Failure<Blog>(errors.ToArray());
}
return new Blog(title);
}
public string Title { get; init; }
}
If we create an instance of Blog
class with a valid title, then returned resule is successful with a new instance of Blog
, but if it's not valid, then we will receive proper error message with detailed information about error.
if (blog.IsSuccess)
{
Console.WriteLine(blog.Value.Title);
}
blog = Blog.Create("");
if (!blog.IsSuccess)
{
foreach (var error in blog.Errors)
{
Console.WriteLine(error);
}
}
blog = Blog.Create("B");
if (!blog.IsSuccess)
{
foreach (var error in blog.Errors)
{
Console.WriteLine(error);
}
}
P.S
This project has been inspired based on Domain Validation With .NET | Clean Architecture, DDD, .NET 6
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.