Skip to content

Instantly share code, notes, and snippets.

@gunnarmorling
Created April 25, 2011 12:20
Show Gist options
  • Save gunnarmorling/940438 to your computer and use it in GitHub Desktop.
Save gunnarmorling/940438 to your computer and use it in GitHub Desktop.
HV-431: New approach by removing creational context from ConstraintDef completely
ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Marathon.class )
.property( "name", METHOD )
.constraint( new SizeDef()
.message( "name too short" )
.min( 3 ) )
.constraint( new NotNullDef() )
.property( "numberOfHelpers", FIELD )
.constraint( new MinDef().value( 1 ) );
ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Marathon.class )
.constraint( GenericConstraintDef.from( MarathonConstraint.class )
.param( "minRunner", 1 ) )
.property( "name", METHOD )
.constraint( new SizeDef()
.message( "name too short" )
.min( 3 ));
@hferentschik
Copy link

WDYT about this:

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Marathon.class )
    .property( "name", METHOD )
        .constraint( SizeDef.create().message( "name too short" ).min( 3 ) )
        .constraint( NotNullDef.create() )
    .property( "numberOfHelpers", FIELD )
        .constraint( MinDef.create().value( 1 ) );

not having the new constructor might also minimize the risk that people keep references to the instantiated def classes

@gunnarmorling
Copy link
Author

Hmmm, people still could keep such references, I don't think there is a way to avoid this. Alternatively one could also have the create method on ConstraintDef instead:

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Marathon.class )
    .constraint( ConstraintDef.createGeneric( MarathonConstraint.class )
        .param( "minRunner", 1 ) )
    .property( "name", METHOD )
        .constraint( ConstraintDef.create(SizeDef.class).message( "name too short" ).min( 3 ) )
        .constraint( ConstraintDef.create(NotNullDef.class )
    .property( "numberOfHelpers", FIELD )
        .constraint( ConstraintDef.create(MinDef.class).value( 1 ) );

That way there is one entry point for instantiating constraint definitions and the creation of "normal" and generic constraint definitions would be somewhat unified. But actually I don't have a strong preference here. I think I'd go for the last version but if you guys team up for one of the other solutions I'm fine with any of them either :)

@hferentschik
Copy link

Sure, we cannot prevent it, but I have the "feeling" it is less likely this way :-)
I actually like the ConstraintDef.create better. It might be a little longer, but it seems indeed more unified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment