I have a hierarchy of data classes
public class Base
{
// Fields to be validated
}
public class Derived1 : Base
{
// More fields to be validated
}
public class Derived2 : Base
{
// More fields to be validated
}
What would be the appropriate way to validated Derived1 and Derived2 using FluentValidation framework without duplicating rules for fields of Base class?
One approach to take would be as follows:
So you first create your base validator, make it accept a generic type argument and specify that the generic type must be of type
base
. Set up your general rules for your base class and move on.For any validators that validate children of your base class, you have those validators inherit from the baseValidator, where T will be your derived class type.