Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Validation and IoC (Unique Field)

I'm developing a web application with asp.net mvc 3 and DDD. For my domain model validation, I've been using Fluent Validation. It's my first project with fluent validation and I'm still learning and modeling the entities.

My entity Customer has two properties that need to be unique in my system, these properties are Email and CPF (it's a Brasilian document and need to be unique in all system). I would like to know, how can I to it ?

Soo, my ideia is, inject (by constructor) my repository in my validation class of Customer and check it by a custom validation. The validation would be check using the repository if is there record in my table with this email different of an Id (0 for inserts and real Id for updates... I don't need to check the record I'm updating because it'd always be true).

I`m trying something like this:

 public class CustomerValidator : AbstractValidator<Customer> {

     protected ICustomerRepository Repository { get; set; }

     // I intend to inject it by IoC with Unity.. is it possible ?
     public CustomerValidator(ICustomerRepository rep) 
     {
         this.Repository = rep;

         // other properties

         RuleFor(customer = customer.Email)
             .EmailAddress()
             .NotEmpty()
             .Must(email = { return Repository.IsEmailInUse(email, ?); });

         RuleFor(customer = customer.CPF)
             .NotEmpty()
             .Must(cpf = { return Repository.IsCPFInUse(cpf, ?); });

     }   }

I don't know if is possible, inject a repository inside the validator, and how could I get the Id in the .Must method extension ? Or is there another method to do it?

like image 426
Felipe Oriani Avatar asked Jul 19 '11 01:07

Felipe Oriani


People also ask

What is fluent validation used for?

What is Fluent Validation? Fluent Validation is a validation library for . NET, used for building strongly typed validation rules for business objects. Fluent validation is one way of setting up dedicated validator objects, that you would use when you want to treat validation logic as separate from business logic.

Is fluent validation good?

Summary. FluentValidation provides a great alternative to Data Annotations in order to validate models. It gives better control of validation rules and makes validation rules easy to read, easy to test, and enable great separation of concerns.

What is fluent validation in .NET core?

Fluent Validation is a free to use . NET validation library that helps you make your validations clean, easy to create, and maintain. It even works on external models that you don't have access to, with ease. With this library, you can separate the model classes from the validation logic like it is supposed to be.

Is fluent validation open source?

Fluent Validation is a popular open source library for solving complex validation requirements written by Jeremy Skinner. You can find the source code and documentation for the library at https://github.com/JeremySkinner/fluentvalidation.


1 Answers

RuleFor(customer => customer.Email)
    .EmailAddress()
    .NotEmpty()
    .Must((customer, email) => Repository.IsEmailInUse(email, customer.Id));

RuleFor(customer => customer.CPF)
    .NotEmpty()
    .Must((customer, cpf) => Repository.IsCPFInUse(cpf, customer.Id));

This being said, checking for uniqueness could also be more efficiently done by the system itself (database?) at the moment you are trying to insert the record and catch the appropriate exception instead of doing this in the validation layer. The reason for this is that between the time your FluentValidation checked for uniqueness and the actual time a record is inserted many things could happen.

like image 92
Darin Dimitrov Avatar answered Oct 07 '22 17:10

Darin Dimitrov