Well, this is due to SOLID 'L' (Liskou Substitution Principle). I suggest you use the following approach:
1) Create an interface, for example:
public IValidator { void Validate(Delegate addErrorMessage); }
2) Create an abstract base class with the abstract DoValidations () method:
public abstract class BaseValidator : IValidator { public void Validate(Delegate addErrorMessage) { DoValidations(addErrorMessage); } protected abstract DoValidations(Delegate addErrorMessage); }
3) Inherit the parent from BaseValidator (or make the parent element a base class instead of BaseValidator):
public class Parent: ValidatorBase { public override void DoValidations(Delegate addErrorMessage) {
4) Inherit the child from the parent:
public class Child : Parent { public override void DoValidations(Delegate addErrorMessage) {
5) Now it's time to add the IsDisabled property. We just need to change ValidatorBase and IValidator:
public interface IValidator { bool IsDisabled {get; set; } void Validate(Delegate addErrorMessage); } public abstract class BaseValidator : IValidator { public bool IsDisabled { get; set; } public void Validate(Delegate addErrorMessage) { if(!IsDisabled) { DoValidations(addErrorMessage); } } protected abstract DoValidations(Delegate addErrorMessage); }
6) Now use your validator IValidator = _factory.Create ():
validator.IsDisabled = false; validator.Validate();
Good luck
source share