Wednesday 8 July 2015

Using FluentValidation in .Net Projects

FluentValidation is a very easy to use simple validation framework which we can use in .Net projects. This is been developed by Jeremy Skinner. It uses  fluent interface and lambda expressions for building validation rules for your business objects.
Following is a simple example of how to use this validation framework in a simple class. 
 class StudentValidator : AbstractValidator
        {
            public StudentValidator ()
            {
                RuleFor(obj => obj.Id).NotEmpty();
                RuleFor(obj => obj.Name).NotEmpty();
                RuleFor(obj => obj.Age).GreaterThan(5).LessThanOrEqualTo(20);
            }
        }

        protected override IValidator GetValidator()
        {
            return new StudentValidator ();
        }

AbstractValidator is fluent class which accepts the class that needs to be validated.


No comments:

Post a Comment