I'm trying to implement a validation framework in a base class for LINQ to SQL entities. The problem I'm having is getting the OnValidate event to fire properly.
The reason is that OnValidate is marked as partial so I can't provide a default implementation in the base class; it gets hidden by a new method declared by the LINQ to SQL class.
How can I fix it so the OnValidate event in the base class will be called automatically?
Example code follows below.
public class EntityBase
{
public bool IsValid
{
get { return (GetValidationErrors().Count() == 0); }
}
public virtual IEnumerable<ValidationError> GetValidationErrors()
{
yield break;
}
public void OnValidate(System.Data.Linq.ChangeAction action)
{
//This never gets fired unless I call explicitly in the derived class.
if (!IsValid)
{
StringBuilder sb= new StringBuilder();
sb.AppendLine("Validation errors prevent saving");
foreach (ValidationError error in GetValidationErrors())
{
sb.AppendLine(String.Format("{0}: {1}", error.PropertyName, error.ErrorMessage));
}
throw new ApplicationException(sb.ToString());
}
}
}
public partial class LinqThingy: EntityBase
{
public override IEnumerable<ValidationError> GetValidationErrors()
{
if (String.IsNullOrEmpty(Name)) yield return new ValidationError("Name required", "Name");
}
//Eww nasty, don't want to have to do this.
partial void OnValidate(System.Data.Linq.ChangeAction action)
{
base.OnValidate(action);
}
}