When writing validation methods on your entity objects, there are two basic approaches:
- Write code inside the attribute setter method for attribute-level validation, and inside an overridden
validateEntity() method for entity-level validation, or
- Add attribute-level and/or entity level declarative method validation rules
I don't recommend approach number one because is complicates error handling, putting the onus on you as the developer to write much more error-handling code to maintain the correct framework behavior -- especially when you are using the BC4J framework's bundled exception mode (used automatically for BC4J/Struts applications).
The declarative validator approach is easier to maintain, offers a consistent mechanism for seeing what business rules have been defined on your object, and requires no additional error handling code to keep the bundled exception mode working correctly.
You can use the built-in "method validator" to add an attribute-level or entity-level business rule that is implemented by invoking a custom Java method in your entity object class. This method validator forces you to name your custom method with a prefix of the name "validate" so it makes all of the custom validation code show up together in the Java Class structure pane (conveniently). Also, having a declarative entry for custom method rules implemented is nice because it means that you can go to the validation panel of your entity object and get a birds-eye view of all business rules that are in effect, regardless of how they are implemented. The custom method validation is great if you are writing validation that is specific only to a single entity object. If you forsee some validations you do turning into a repeatable validation "pattern" (perhaps differing each time only by some parameterizable values), then you can create your own reusable business rules, and then many different entities can reuse those rules declaratively.
The BC4J framework comes with a handful of built-in rules, but the real powerful feature is that you can extend the set. The BC4J Toy Store Demo application features documented and explained examples of both method validator rules as well as a custom validation rule reused in multiple entity objects.
You can use the built-in method validator to declarative invoke validation methods that you write
2:36:48 PM
|