I am developing a web application with asp.net mvc 3 and DDD. To test my domain model, I use Fluent Validation. This is my first smooth check project, and I'm still exploring and modeling entities.
My Customer object has two properties that must be unique in my system, these properties are email and CPF (this is a Brazilian document and must be unique throughout the system). I would like to know how can I do this?
Su, my idea, embeds (by constructor) my repository in my Client validation class and validates it using a special verification. The check will be checked using the repository if there is a record in my table with this address other than Id (0 for attachments and a real identifier for updates ... I do not need to check which record I am updating, d is always true).
I am trying something like this:
public class CustomerValidator : AbstractValidator<Customer> {
protected ICustomerRepository Repository { get; set; }
public CustomerValidator(ICustomerRepository rep)
{
this.Repository = rep;
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 it’s possible to insert the repository inside the validator and how can I get the identifier in the extension of the .Must method? Or is there another way to do this?
source
share