Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IValidator<T>
- {
- bool IsValid(T entity);
- IEnumerable<string> BrokenRules(T entity);
- }
- public class OrderPersistenceValidator : IValidator<Order>
- {
- public bool IsValid(Order entity)
- {
- return BrokenRules(entity).Count() > 0;
- }
- public IEnumerable<string> BrokenRules(Order entity)
- {
- if (entity.Id < 0)
- yield return "Id cannot be less than 0.";
- if (string.IsNullOrEmpty(entity.Customer))
- yield return "Must include a customer.";
- yield break;
- }
- }
- person.Address = "123 my street";
- person.City = "Houston";
- person.State = "TX";
- person.Zip = 12345;
- person.ChangeAddress(.......);
- bool IsVerifiedBy(TEntity candidate)
- IEnumerable<string> BrokenRules(TEntity canditate)
- bool IsVerifiedBy(TEntity candidate)
- {
- return BrokenRules(candidate).IsEmpty();
- }
- IEnumerable<string> BrokenRules(TEntity candidate)
- {
- if (someComplexCondition)
- yield return "Message describing cleary what is wrong...";
- if (someOtherCondition)
- yield return
- string.Format("The amount should not be {0} when the state is {1}",
- amount, state);
- }
Add Comment
Please, Sign In to add comment