Hi Kioko,
As the EmployeeEntity inherits from PersonEntity when you CreateValidator for Employee, this is used as the validator of the entity. You wont get two validators called.
One thing you could do is Inherit the validator. I will explain with an example:
- Create the validator for PersonEntity:
public class PersonValidator : ValidatorBase
{
public DummyValidatorBase()
{
}
public override void ValidateEntityBeforeSave(IEntityCore involvedEntity)
{
// validate things
//...
// validate base
base.ValidateEntityBeforeSave(involvedEntity);
}
}
- Create a Validator for EmployeeEntity. This validator will inherit from PersonValidator
public class EmployeeValidator : PersonValidator
{
public DummyValidatorBase()
{
}
public override void ValidateEntityBeforeSave(IEntityCore involvedEntity)
{
// validate things
//...
// validate base (PersonEntity)
base.ValidateEntityBeforeSave(involvedEntity);
}
}
- In PersonEntity override CreateValidator this way:
protected override IValidator CreateValidator()
{
return PersonValidator();
}
- In EmployeeEntity override CreateValidator this way:
protected override IValidator CreateValidator()
{
return EmployeeValidator();
}
This is the recommended approach. This way you have flexibility. Look at the possible cases:
A. When you instantiate and save only PersonEntity, only ValidateEntityBeforeSave method of the PersonValidator is called.
B. When you instantiate and save an EmployeeEntity, the ValidateEntityBeforeSave method of the EmployeeValidator is called, and the ValidateEntityBeforeSave of the PersonEntity as well. You can call the base.ValidateEntityBeforeSave before or after your custom employee validation rules are performed.
C. If you want to customize the validation rules of the employeeEntity, you can do that in the EmployeeValidator, without disturbing the PersonValidator.
Hope helpful