Validation is a tough subject. One that I'm constantly trying to think of better ways of doing. Some suggest that all validation should occur in the domain, and some prefer to check if the object is valid before proceeding. I lean towards the idea of not allowing your objects to enter an invalid state. So far the easiest approach I have found to do this is to raise meaningful exceptions in the domain to ensure this.

However, when there are several reasons why an object can be considered "invalid" and, those reasons need to be reflected in the UI, I haven't been able to figure out a clean way to do this in the domain. Suggestions are welcome.

Here's an approach that we've taken to some of our validation, when user input needs to be checked so that we can provide meaningful error messages to the end user.

First we have 2 core validation interfaces:

1 public interface IValidationResult
2     {
3         bool IsValid { get; }
4         IEnumerable<string> BrokenRules { get; }
5     }

and

1 public interface IValidation<T>
2     {
3         IValidationResult Validate(T item);
4     }

The IValidation is in essence a form of a Specification. Now to collect the errors we use a [visitor](http://www.mokhan.ca/oo/designpatterns/2010/12/14/the-visitor-design-pattern.html). The following are the core [visitor](http://www.mokhan.ca/oo/designpatterns/2010/12/14/the-visitor-design-pattern.html) interfaces.

 1 public interface IVisitor<T>
 2   {
 3       void Visit(T item_to_visit);
 4   }
 5 
 6   public interface IValueReturningVisitor<TypeToVisit, TypeToReturn> : IVisitor<TypeToVisit>
 7   {
 8       void Reset();
 9       TypeToReturn Result { get; }
10   }

We have an implementation of a IValueReturningVisitor that collects errors from visiting IValidations, then returns a validation result.

 1 public class ErrorCollectingVisitor<T> : IValueReturningVisitor<IValidation<T>, IValidationResult>
 2   {
 3       readonly T item_to_validate;
 4       readonly List<string> results;
 5 
 6       public ErrorCollectingVisitor(T item_to_validate)
 7       {
 8           this.item_to_validate = item_to_validate;
 9           results = new List<string>();
10       }
11 
12       public void Visit(IValidation<T> item_to_visit)
13       {
14           var validation_result = item_to_visit.Validate(item_to_validate);
15           if (!validation_result.IsValid)
16           {
17               results.AddRange(validation_result.BrokenRules);
18           }
19       }
20 
21       public void Reset()
22       {
23           results.Clear();
24       }
25 
26       public IValidationResult Result
27       {
28           get { return new ValidationResult(results.Count == 0, results); }
29       }
30   }

And a handy extension method for returning the value from visiting a set of validations.

1 public static Result ReturnValueFromVisitingAllItemsWith<TypeToVisit, Result>(this IEnumerable<TypeToVisit> items_to_visit, IValueReturningVisitor<TypeToVisit, Result> visitor)
2   {
3       visitor.Reset();
4       items_to_visit.Each(x => visitor.Visit(x));
5       return visitor.Result;
6   }

An example of the usage for the visit can be seen below:

1 public IValidationResult Validate(IUser user)
2   {
3       return userValidations
4           .Select(x => x as IValidation<IUser>)
5           .ReturnValueFromVisitingAllItemsWith(new ErrorCollectingVisitor<IUser>(user));
6   }
comments powered by Disqus