I working with FluentValidation and EF Migrations.
Basically the EF Migrations have a class
Database settings
View model validations
The idea is to create a plugin that created a AbstractValidator for each configuration of the EF Migrations with the rules already defined.
I thought of an abstract class, something like:
New way
This is because in many cases the data class is often different of view class (VM)
Basically the EF Migrations have a class
Complex/EntityTypeConfiguration
that sets the constraints in the database (Required, Max length etc).Database settings
public class TestimonyConfiguration : EntityTypeConfiguration<Testimony>
{
public TestimonyConfiguration()
{
Property(p => p.Name).IsRequired().HasMaxLength(70);
Property(p => p.Email).IsRequired().HasMaxLength(126);
Property(p => p.Message).IsRequired();
}
}
So in my application I create a class to validate this object:View model validations
public class TestimonyValidator : AbstractValidator<Testimony>
{
public TestimonyValidator()
{
RuleFor(p => p.Name).NotEmpty().Length(0, 70);
RuleFor(p => p.Email).NotEmpty().EmailAddress().Length(0, 126);
RuleFor(p => p.Message).NotEmpty();
}
}
Did you see the similarity?The idea is to create a plugin that created a AbstractValidator for each configuration of the EF Migrations with the rules already defined.
I thought of an abstract class, something like:
New way
public class TestimonyValidator : EntityTypeValidator<Testimony /* Type to validate */, Testimony /* Entity to get configuration */>
{
public TestimonyValidator()
{
// All rules will be created based on the configuration of this object
// You can add new rules
}
}
Important to emphasize the possibility of seeking another class settings.This is because in many cases the data class is often different of view class (VM)