tags:

views:

90

answers:

2

We use spring validation to validate our models before stuffing them into the database . So for example an

Order --> OrderItem

before saving the order to the database it is validated by OrderValidator

OrderValidator validates order delegates to OrderItemValidator for validating OrderItem with the Order. So the hierarchy or validators in this case looks like

OderValidator --> OrderItemValidator

Knowledge that order has orderItems is present in both validator and the model.

I am wondering if this is a sign of implicit duplication ( something similar to Parallel inheritance hierarchy code smell ). If yes , what are the ways I could avoid it ?

+1  A: 

I would look at dividing the validation process into a class-agnostic function, and parameters per class that the real class can use with the validation function to basically check itself. Viability of that approach requires more knowledge of the problem space than has been provided.

staticsan
A: 

In my experience validators are best implemented using some kind of decorator. They shouldn't be inheriting from anything, as validators don't have a rational is-a relationship with each other.

Validators merely look at data incidental to a model, and not the model's behavior, so I don't think any heirarchy would be appropriate at all.

Jon Limjap