Tools for building business domain Model Objects (MOs).
The important things about Model Objects in WEB4J are :
- Model Objects are immutable (highly recommended, but not required)
- Model Objects implement their own validation logic
- in general, Model Objects are not dumb data carriers.
- if desired, Model Objects can easily avoid the Java Beans anti-pattern.
Restrictions on Model Objects
These are the restrictions on Model Objects in WEB4J :
- they must be public classes
- the constructors must be public, and always take one or more arguments
- the constructors perform all validation, and must throw
{@link hirondelle.web4j.model.ModelCtorException} if any problems occur
- to be easily used with WEB4J utility classes, the constructor arguments must belong
to a set of common building block classes (Integer, BigDecimal, and so on)
These items can be added to Model Objects, if desired, but they are never used by WEB4J :
- no-argument constructors
- setXXX methods
Validation
Validation in Model Objects is always in the constructor, and can usually be implemented using the
{@link hirondelle.web4j.model.Check} class, which provides commonly needed implementions of the {@link hirondelle.web4j.model.Validator} interface.
When a problem occurs, an error message is added to {@link hirondelle.web4j.model.ModelCtorException}, for
later presentation to the user.
Object Methods
It is highly recommended that all Model Objects override equals, hashCode, and
toString. ({@link hirondelle.web4j.model.ModelUtil} can help you implement these methods.)
Building Model Objects
In general, there are two sources for the data attached to Model Objects : manual entry by the end user, and the database.
It is interesting that both of these sources should be treated as unreliable. Incoming HTTP request
parameters are not constrained, and must always be aggressively validated on the server. Databases are independent
servers, and cannot be assumed to be 'owned' by the application. Other applications and processes can interact
with the data as well. Thus, an application cannot, in general, make any assumptions regarding the
content of the database.
Thus, a Model Object must allow for all possible input when creating objects from both these sources.
WEB4J has two main tools for this task :
- {@link hirondelle.web4j.model.ModelFromRequest}, for building Model Objects from underlying request parameters
- {@link hirondelle.web4j.database.Db}, for building Model Objects from an underlying ResultSet
Both of these tools are simple to use because they use effective ordering conventions for data.