DDD Using Specification pattern for Validation

前端 未结 2 1342
栀梦
栀梦 2020-12-04 16:16

I am thinking of using Specification pattern for validation purposes. The hard thing is how to tell the user why some Specification was not satisfied. What if the Spec

2条回答
  •  甜味超标
    2020-12-04 16:23

    I had the same problem. I create Validation decorator for Specification (code is JAVA).

      interface Validator{
        Respond validate(T t)
      }
    
    
      class abstract ValidationSpecificationDecorator implements Validator {
      Specification spec;
    
      ValidationSpecificationDecorator(Specification spec){
        this.spec =  spec;
      }
    
      public Respond  validate(T t) {
        Respond respond = new Respond();
        if(!spec.IsSatisfiedBy(t){
           respond.add(error(t));
        }
        return respond;
      )
    
      public abstract Error error(T t);
    
      }
    

提交回复
热议问题