Well, my problem is that I am creating an api using aspnetcore 2.1, to avoid code duplication I have created an abstract class with the properties that share the dtos (board, boardforcreation, boardforupdate, etc). I added to the abstract class personalized validation using ivalidatableobject, now, I want to add personalized validation to the classes that derive from the abstract class but it tells me that extending from ivalidatableobject interface is rebundant because it was already declared in the base class and also when I add the Validate method in the derived class it tells me that it is already declared and implemented, then, how can I add validation in an abstract class and in a derived class using ivalidatableobject? or is there another way to achieve this. Thank you in advance.
public class Board : BoardAbstractBase, IValidatableObject
{
public Guid BoardId { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset EndDate { get; set; }
}
public abstract class BoardAbstractBase : AbstractBasicEntity, IValidatableObject
{
public DateTimeOffset EstimatedStartDate { get; set; }
public DateTimeOffset EstimatedEndDate { get; set; }
public decimal EstimatedBudget { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (!(EstimatedStartDate < EstimatedEndDate))
yield return new ValidationResult(
"StartDateBeforeEndDate|The estimated start date should be smaller than the end date.",
new[] {"BoardAbstractBase"});
}
}
Add a virtual method to your base class.
If you want to perform some common validation logic in the base class as well as perform extra validation logic in each of the concrete implementations, then add an virtual validation method to your base class that is called within the base class validation function.
Add to your base class:
Then in each concrete implementation implement
RepoValidatewith whatever custom validation logic you need asprotected override bool RepoValidate() {...}.For Example
Then in
BoardAbstractBase.Validate:Now, you can always modify
RepoValidateto return a validation result if it fails, or take any argument, but just for the sake of example, this one simply returns false. Also, because it isvirtualand notabstract, you only need to override it when you have extra custom logic to perform.