[hibernate-issues] [Hibernate-JIRA] Commented: (HV-274) Write a programmatic mapping API for constraint declarations

Emmanuel Bernard (JIRA) noreply at atlassian.com
Wed May 12 03:49:24 EDT 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HV-274?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=37073#action_37073 ] 

Emmanuel Bernard commented on HV-274:
-------------------------------------

Mandatory parameters are nice and should be respected as much as possible but there are alternatives to what Josh Bloch describes. Here is an approach that simulates named parameters: in Hardy's approach you could make sure that onField and onGetter return types have no other method but ofType. Basically the sure is not done with the configuration unless it goes all the way through.

Otherwise, why placing the constraint declaration in front in the API navigation? It prevents people from adding several constraints on a given field.

{code}
config
  .type(ShoppingCart.class)
  .addConstraint(MyClassConstraint.class)
    .getter("items")
    .addConstraint(Size.class)
      .withParameter("min", 0)
{code}

This might require a closing context method though (.configure(), .freeze(), ...) to know that the declaration of the last constraint is done. Not sure.

withXXX / addYYY pairs are hard to code when they can be interchanged ie

{code}
constraint.withMessage("doom!").andParameter("heat", 2000)
//or
constraint.withParameter("doom!").andMessage("heat", 2000)
{code}

They require complex return type combinations, I would not go there.

Finally, would there be a way to make parameters type-safe for the built-in and Hibernate Validator constraints as well as all the constraint definitions that play along. For each constraint X, we would get a companion class XDefinition that reproduces the parameter list and even potentially enforce mandatory parameters

{code}
config
  .type(ShoppingCart.class)
    .getter("items")
      .constraint(NotNullDefinition.class)
      .constraint(SizeDefinition.class)
        .min(3).max(10);
    .getter("price")
      .constraint(RangeDefinition.class)
        .min(0)
        .max(100)

class SizeDefinition {
  OptionalInDefinition<SizeDefinition> min(int min) {...}
  OptionalInDefinition max(int max) {...}
}

class RangeDefinition {
  MaxContext min(int min) {...}
  public static class MaxContext {
    OptionalsInDefinition<?> max(int max) {...}
  }
}
{code}

PS: this is a draft example it probably does not work and needs refinement.

We could also add hardwired approaches to built-in types

{code}
config
  .type(ShoppingCart.class)
    .getter("items")
      .size().max(30)
      .notNull()
        .forGroups(MustBuy.class)
{Code}

> Write a programmatic mapping API for constraint declarations
> ------------------------------------------------------------
>
>                 Key: HV-274
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-274
>             Project: Hibernate Validator
>          Issue Type: New Feature
>          Components: engine
>    Affects Versions: 4.0.2.GA
>            Reporter: Emmanuel Bernard
>            Assignee: Hardy Ferentschik
>             Fix For: 4.1.0
>
>


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list