Hello,

Is that the way that it is intended to work?  I thought I read 
somewhere, though I can't find it now, that the constraints would be 
checked at the end of the unit of work.  I thought this would be a nice 
way to allow my validation to happen in the domain layer and propagate 
the violation messages to the application for them to be displayed to 
the user. 

The way it is currently implemented only the first constraint violation 
would be reported to the user, making it unusable as a general way of 
doing validation as it would require the user to submit the form, fix 
the first validation error, submit the form, fix the next validation 
error, ad nauseum as opposed to having all the constraint violations 
checked at the end of the unit of work and reporting all violations at 
once.  I could duplicate the constraints in the application before 
passing the parameters to the domain layer, but that just seems ugly and 
was one problem I was hoping Qi4j would help me solve.

The other problem with this mechanism is that it requires a setter to be 
called for any validation to be performed.  The problem with this is 
that it could allow invalid data to pass.  For instance, in the Nameable 
example that I've used elsewhere we have

public interface Nameable {
    @NonEmptyString Property<String> name();
}

That non-empty string constraint is only ever going to be checked when 
nameable.name().set(something) is called.  If someone where to simply 
create the entity and never call that setter, we'd wind up with an 
invalid object in our entity store.

There are 2 ways to fix it, leave the current checks as they are and do 
the constraint checks again when the unit of work completes or remove 
the check on the setter and add the checks to the unit of work 
completion and rely on it exclusively.

WDYT?

Rich

Rickard Öberg wrote:
> Hey,
>
> I think QI-59 is fixed now. EntityPropertiInstance simply calls 
> super.set(aNewValue), which will invoke the constraints checking in 
> PropertyInstance.
>
> Testcase that now works:
> @Test
> public void 
> givenEntityWithConstrainedPropertyWhenInvalidPropertyValueSetThenThrowException()
> {
>      UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
>
>      try
>      {
>          TestCase testCase = uow.newEntity( TestCase.class );
>
>          testCase.someProperty().set( null );
>
>          uow.complete();
>          fail( "Should not be allowed to set invalid property value" );
>      }
>      catch( Exception e )
>      {
>          uow.discard();
>      }
> }
>
> interface TestCase
>      extends EntityComposite
> {
>      @NotNull Property<String> someProperty();
> }
> ---
>
> /Rickard
>
> _______________________________________________
> qi4j-dev mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/qi4j-dev
>   


_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to