Fluent Validation in C#.NET


🚀 Simplify Data Validation with FluentValidation in C# 📝

Are you tired of writing cumbersome and error-prone validation logic in your C# projects? Enter FluentValidation, a powerful library that simplifies and enhances data validation, making your code cleaner and more robust.

Let's dive into a demonstration using FluentValidation to validate a Student model in a hypothetical console application.

Step 1: Defining the Student Model and Validation Rules

using FluentValidation;

namespace FluentAppConsole.Model

{

    public class Student

    {

        public int Id { get; set; }

        public int Age { get; set; }

        public string Name { get; set; }

        public string FatherName { get; set; }

        public string Email { get; set; }

    }


    public class StudentValidation : AbstractValidator<Student>

    {

        public StudentValidation()

        {

            RuleFor(x => x.Id).NotEmpty();

            RuleFor(x => x.Age).NotEmpty().WithMessage("Please enter age").InclusiveBetween(18, 60).WithMessage("Please enter your age between 18-60");

            RuleFor(x => x.Name).NotEmpty().Length(10);

            RuleFor(x => x.FatherName).NotEmpty().MaximumLength(20).MinimumLength(6);

            RuleFor(x => x.Email).NotEmpty().WithMessage("Please enter your email").EmailAddress().WithMessage("Email is not valid");

        }

    }

}


Step 2: Implementing Validation in the Console Application


using FluentAppConsole.Model;

Console.WriteLine("Welcome to FluentValidation program");

var myStudent = new Student
{
    Id = 1,
    Age = 17,
    Email = "Hammad",
    FatherName = "Muhammad Maqbool",
    Name = "Hammad Maqbool"
};

var myValidator = new StudentValidation();
var validationResult = myValidator.Validate(myStudent);

if (!validationResult.IsValid)
{
    Console.WriteLine("Validation failed. Errors:");
    foreach (var error in validationResult.Errors)
    {
        Console.WriteLine($"- {error.ErrorMessage}");
    }
}

Understanding the Validation Process In this example, we define a Student model with properties like Id, Age, Name, FatherName, and Email. The StudentValidation class extends AbstractValidator and specifies validation rules for each property using FluentValidation's expressive syntax.

In the console application, we create an instance of a Student and set its properties with hypothetical values. Then, we instantiate the StudentValidation class and use Validate() to validate the Student object against the defined rules.

If any validation fails, the errors are captured in the validationResult.Errors collection, providing detailed error messages for each failed validation rule.

Conclusion FluentValidation simplifies and streamlines data validation in C# applications. By defining clear and concise validation rules, you can ensure data integrity and improve code quality, making your applications more robust and user-friendly.

Start integrating FluentValidation into your projects today and experience hassle-free data validation like never before!

🌟 Share your experiences with FluentValidation! #FluentValidation #CSharp #CleanCode

Post a Comment

Previous Post Next Post