Fluent validation
-
Upload
saravanan-subburayal -
Category
Technology
-
view
1.523 -
download
3
description
Transcript of Fluent validation
FLUENT VALIDATION
SARAVANAN SUBBURAYALJEEVAN TECHNOLOGIES
07-MAR-2013
Agenda
• Fluent Validation – Who am I?• Validator Creation• Built in Validators• Validator customizations• Localization support• Benefits• Conclusion
Fluent Validation – Who am I?
• Open source Validation library for .net• Uses Fluent interfaces and lambda expressions for
building validation rules• Source available in GitHub• Available as Nu-get packages– FluentValidation– FluentValidation.MVC3– FluentValidation.MVC4
• Signed binaries are also available
Validator Creation
• Validator class : AbstractValidator<>• Validation Result– IsValid– Errors
• Chaining Validators• Throwing Exceptions• Validators for Complex properties• Validators for Collections• Rule Sets
Built in Validators
• NotNull Validator• NotEmpty Validator• NotEqual Validator• Equal Validator• Length Validator• Less Than Validator
Built in Validators (contd.,)
• Less Than Or Equal Validator• Greater Than Validator• GreaterThan Or Equal Validator• Predicate Validator (aka Must)• RegEx Validator• Email Validator
Validator customizations
• Overriding the default error message– .WithMessage(“…”)
• Overriding the Default Property Name– .WithName(“Family name")
• Specifying a condition (When/Unless)– .When(Student.Age > 30)
• Cascading– .Cascade(CascadeMode.StopOnFirstFailure)
Localization support
• It supports the following languages:– English– French– German– Dutch– Spanish– Portuguese
Localization support (contd.,)
• Using Custom Resource Provider
• Messages must be public and static
Localization support (contd.,)
• Resource names that can be customizedemail_error equal_error exact_length_error exclusivebetween_errorgreaterthan_error greaterthanorequal_errorinclusivebetween_error length_errorlessthan_error lessthanorequal_errornotempty_error notequal_errornotnull_error predicate_errorregex_error
Benefits
• It separates validation from the Model• Unit testing is easier• Can perform different conditional validations
on different properties• Re-usability• No need for annotations (which make the
code looks clumsy)
DEMO
If (!Questions){ “THANK YOU!!!”;}