C# – LINQ to SQL: OnValidate() and custom domain model classes

asp.net-mvcclinq-to-sql

Working through the NerdDinner Tutorial, I'm trying to figure out a good way to perform validation on properties that isn't dependent on a LINQ-to-SQL generated partial class. Here's some example code of what I've done so far:

public abstract class DomainEntity
{
    public IEnumerable<ValidationError> ValidationErrors { get; private set; }

    public bool Validate()
    {
        bool isValid = false;

        if (this.ValidationErrors != null)
            this.ValidationErrors = null;

        this.ValidationErrors = this.GetValidationErrors();

        if (this.ValidationErrors.Count() == 0)
            isValid = true;

        return isValid;
    }

    protected abstract IEnumerable<ValidationError> GetValidationErrors();
}

public partial class Email : DomainEntity
{
    protected override IEnumerable<ValidationError> GetValidationErrors()
    {
        if (!this.ValidateAddress())
            yield return new ValidationError("Address", DomainResources.EmailAddressValidationErrorMessage);

        yield break;
    }

    partial void OnValidate(ChangeAction action)
    {
        bool isValid = this.Validate();

        if (!isValid)
            throw new InvalidEmailException(this);
    }

    private bool ValidateAddress()
    {
        // TODO: Use a regex to validate the email address.

        return !string.IsNullOrEmpty(this.Address);
    }
}

Where Email is a LINQ-to-SQL generated type based off an Email table. Since the Email table is but one of several entities related to a domain model class (say, "User"), the ideal is to create a "User" domain model class and use the Validation Application Block attributes to validate properties. In other words, I'd like to use this:

public class User
{
    private Email emailEntity;

    [EmailAddressValidator]
    public string EmailAddress
    {
        get { return emailEntity.Address; }
        set { emailEntity.Address = value; }
    }
}

So that if I change my database schema, and the changes fall through my LINQ-to-SQL generated classes, I don't have these orphaned partial classes (like partial class Email). I also want the benefit from integrating the Validation Application Block attributes, so that I don't have to maintain a collection of regexes, as is done in the NerdDinner tutorial. Plus, User as a domain class is going to be the functional unit in the domain, not Email and other entities, for creating view models, rendering views, etc. However, there's no way to capture the Validation call without doing something like:

public abstract class DomainEntity
{
    public event EventHandler Validation(object sender, EventArgs args);
    protected void OnValidation()
    {
        if (this.Validate != null)
            this.Validate(this, EventArgs.Empty);
    }
}

public partial class Email
{
    partial void OnValidate(ChangeAction action)
    {
        this.OnValidation();
    }
}

And then having User hook into that event and handle all the validation within User. Would that even work well with the Validation Application Block? How to perform validation in aggregated domain classes like User in a sensible way?

Best Answer

Treat validation as a service rather than as a responsibility of the entity, this will let you separate implementation of the validation from the definition of what is valid and turn validation into an explicit operation rather than an implicit one ( managed by L2S ).

Have a look at fluent validation for .net ( http://www.codeplex.com/FluentValidation ) for a good implementation of this approach.

Related Topic