I am trying to use Microsoft corporate network validation methods to perform validation on my entities. In my base class, I have the following method:
public class BaseEntity
{
public bool IsValid()
{
return Validate().IsValid;
}
public ValidationResults Validate()
{
return Validation.Validate<this.GetType()>(this);
}
The problem is that even if a subclass of BaseEntity calls IsValid, this.GetType () always returns BaseEntity, not the type of the subclass. I do not want to rewrite this code for each object, as it seems very non-OO. Is there any other way to do this?
I had the idea of ββprotecting a protected variable of type _validationType and setting it to the value of this .GetType object in every object, but it seems like there should be a better way to do this.
Update
It doesn't seem to matter. this.GetType () seems to work the way I hoped. Not sure why this was not before.
Validate(), :
return ValidationFactory.CreateValidator(this.GetType()).Validate(this);