Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation Design Pattern

I am wrting a data validation utility for one of our department which has following requirement. - Dynamically adding new business entity - Dynamically adding new validations to an entity. - An UI to display list of business entity and their validaiton - User will have option to start the validation on all or selcted business entity validaiton. - UI will display a validation error message if any validation fails. - System should proceed to the next validation even if any of the validation fails thus all configured validaiton are validated.

After searching internet I found following 2 promissing design pattern which satisfy my business requirement one id Decorator pattern and another is Chain of Command (aka Chain of Responsibilty). Now my question is which is better? Anyone got any better idea?

Thanks

like image 507
TheITGuy Avatar asked Dec 07 '10 12:12

TheITGuy


1 Answers

I think what you want is the Specification Pattern. So you would do something like this:

public void StartDateNotInPastSpecification : ISpecification<ISomeBusinessObject>
{
  public bool IsSatisfiedBy(ISomeBusinessObject myBusinessObject)
  {
    return myBusinessObject.StartDate >= DateTime.Now;
  }
}

The nice thing about this pattern is that each rule is easily testable in isolation and you get to choose when to apply the validation rules (as opposed to some frameworks which impose this decision on you).

like image 58
Josh Kodroff Avatar answered Sep 30 '22 20:09

Josh Kodroff