<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Did you end up playing with forMethod vs getMethodValidator ?&nbsp;I must admit, I'd like to see them in action before choosing.<div><br></div><div>What do others think. this is a decision between the following two approaches:</div><div><br></div><div>## Fluent approach</div><div><br></div><div><div>public interface Validator {</div><div>&nbsp; &nbsp; MethodValidator&lt;T&gt; forMethod(Method method, T object);</div><div>&nbsp; &nbsp; MethodValidator&lt;T&gt; forConstructor(Constructor&lt;T&gt; constructor);</div><div>}</div><div><br></div><div>public interface MethodValidator&lt;T&gt; {</div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateParameter(</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Object parameterValue, int parameterIndex, Class&lt;?&gt;... groups);</div><div><br></div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateAllParameters(</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Object[] parameterValues, Class&lt;?&gt;... groups);</div><div><br></div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateReturnValue(</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Object returnValue, Class&lt;?&gt;... groups);</div><div>}</div><div><br></div><div>## Getter approach</div><div><br></div><div><div>public interface Validator {</div><div>&nbsp; &nbsp; MethodValidator getMethodValidator();</div><div>}</div><div><br></div><div>public interface MethodValidator&lt;T&gt; {</div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateMethodParameter(</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Method method, T object, Object parameterValue, int parameterIndex, Class&lt;?&gt;... groups);</div><div><br></div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateAllMethodParameters(</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Method method,&nbsp;T object,&nbsp;Object[] parameterValues, Class&lt;?&gt;... groups);</div><div><br></div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateMethodReturnValue(</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Method method,&nbsp;T object,&nbsp;Object returnValue, Class&lt;?&gt;... groups);</div><div><br></div><div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateConstructorParameter(</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Constructor&lt;T&gt; constructor, Object parameterValue, int parameterIndex, Class&lt;?&gt;... groups);</div><div><br></div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateAllConstructorParameters(</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Constructor&lt;T&gt; constructor,&nbsp;Object[] parameterValues, Class&lt;?&gt;... groups);</div><div><br></div><div>&nbsp; &nbsp; &lt;T&gt; Set&lt;MethodConstraintViolation&lt;T&gt;&gt; validateConstructorReturnValue(</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Constructor&lt;T&gt; constructor,&nbsp;Object returnValue, Class&lt;?&gt;... groups);</div></div><div>}</div></div><div><br></div><div>## Comparison</div><div><br></div><div>The advantage of segregation is to avoid pollution of the main&nbsp;Validator&nbsp;interface especially if we add more methods in the future.<br><br>The getter approach has the benefit of being simple.<br><br>The fluent approach let's us factor the various methods between the methods and constructor calls and makes names less awkward. It also is&nbsp;consistent with some of the Bean Validation design using fluent APIs.<br>The drawback of of fluent API is that it requires two method calls for a validation:<br><div><span class="Apple-tab-span" style="white-space:pre">        </span>• one to select the object and method<br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>• one to validate the parameters / return value<br></div>It also creates a temporary object (returned by the first method).</div><div><br></div><div>I have captured this discussion at&nbsp;<a href="http://beanvalidation.org/proposals/BVAL-241/#validating">http://beanvalidation.org/proposals/BVAL-241/#validating</a></div><div>"DISCUSSION: Would a separate interface&nbsp;MethodValidator&nbsp;make sense?"</div><div><br></div><div>Emmanuel</div><div><br><div><div>On 1 août 2012, at 08:20, Gunnar Morling wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><p>Hi,</p><p>What do you other guys think?</p><p>I'll go and create a branch to play around a bit with a separate MethodValidator interface. Maybe it helps to have something more specific which we then can compare and discuss.</p><p>--Gunnar</p>
<div class="gmail_quote">Am 26.07.2012 10:38 schrieb "Hardy Ferentschik" &lt;<a href="mailto:hardy@hibernate.org">hardy@hibernate.org</a>&gt;:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
On Jul 25, 2012, at 12:00 AM, Gunnar Morling wrote:<br>
<br>
&gt; Hi,<br>
&gt;<br>
&gt; 2012/7/23 Hardy Ferentschik &lt;<a href="mailto:hardy@hibernate.org">hardy@hibernate.org</a>&gt;:<br>
&gt;&gt; Hi all,<br>
&gt;&gt;<br>
&gt;&gt; Let me pick up yet another TODO from the current spec.<br>
&gt;&gt;<br>
&gt;&gt; Section "5.1.2. Method-level validation methods" [1] still contains a TODO whether the methods for method validation should be hosted<br>
&gt;&gt; on a different interface (other than javax.validation.Validator).<br>
&gt;&gt;<br>
&gt;&gt; At the moment all validation methods are hosted on javax.validation.Validator. Personally I don't see a strong reason for introducing<br>
&gt;&gt; another indirection/interface. Does anyone have objections removing the todo?<br>
&gt;<br>
&gt; I guess Emmanuel does :)<br>
&gt;<br>
&gt; Personally, I also used to be of the opinion that a separate interface<br>
&gt; doesn't really add much value. What made me pondering though was the<br>
&gt; recent discussion about adding new bean validation methods such as<br>
&gt; validateProperty(T object, Path property, Class&lt;?&gt;... groups);<br>
&gt;<br>
&gt; Following the interface segregation principle [1], it may indeed be a<br>
&gt; good idea to have two separate interfaces, one for standard bean<br>
&gt; validation and one for method validation. I think the main question<br>
&gt; is, who the consumers of the individual methods are. I think there may<br>
&gt; be a broader range of users of the bean validation methods<br>
&gt; (validate(), validateProperty() etc.) than of the method validation<br>
&gt; methods (validateParameters() etc.), which typically will only be<br>
&gt; invoked by authors of integration/glue code. So for users of the first<br>
&gt; group it would reduce complexity if the method validation stuff went<br>
&gt; into a separate interface.<br>
&gt;<br>
&gt; With respect to retrieving method validators, instead of something<br>
&gt; like Validator#forMethod(Method method) etc. I could also imagine<br>
&gt; ValidatorFactory#getMethodValidator(). Then one doesn't have to<br>
&gt; retrieve a new validator for each validated method/constructor.<br>
<br>
+1 for ValidatorFactory#getMethodValidator() in case we decide separate interfaces<br>
<br>
<br>
--Hardy<br>
<br>
<br>
_______________________________________________<br>
beanvalidation-dev mailing list<br>
<a href="mailto:beanvalidation-dev@lists.jboss.org">beanvalidation-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
</blockquote></div>
_______________________________________________<br>beanvalidation-dev mailing list<br><a href="mailto:beanvalidation-dev@lists.jboss.org">beanvalidation-dev@lists.jboss.org</a><br>https://lists.jboss.org/mailman/listinfo/beanvalidation-dev<br></blockquote></div><br></div></div></body></html>