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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With