Skip to content

CodeRADModels

Steve Hannah edited this page Jun 30, 2021 · 2 revisions

CodeRAD Models

Every app will define a set of classes to model it’s data. Sometimes these are referred to as "Business Objects", or "Domain models", or "Entities". For example, in a To Do List app, you would probably have a class called ToDoItem (or equivalent), that encapsulates an item from the To Do list.

You might begin by using POJOs (Plain old Java Objects) for these models, but it doesn’t take long before you feel the need for more than POJOs offer out of the box. At a minimum, domain models should be observable so that views can be notified of changes to the model, and can update themselves in response.

CodeRAD models support fine-grained observability and loose-coupling, making it possible for views to "bind" to their properties, and update themselves in response to changes.

Models are defined by extending the Entity interface, and adding the @RAD annotation. The CodeRAD annotation processor, will generate two concrete implementations of your model: a default implementation, and a "wrapper" implementation.

EntityAnnotationProcessorFlow
Figure 1. A diagram showing the CodeRAD annotation processor’s generation of concrete implementations of your Model interface.
An example model implementation that defines a single property: "name".
import com.codename1.rad.models.Entity;
import com.codename1.rad.schemas.Person;
import com.codename1.rad.annotations.RAD;

@RAD
public interface MyModel extends Entity {

    // Tagging the "name" property with the Person.name
    // Allows for loose-coupling with views.  The view can "bind"
    // to the Person.name tag rather than directly to this
    // property.
    @RAD(tag="Person.name")
    public String getName();
    public void setName(String name);

    // .. define more properties
}

Creating New Instances of Model

The CodeRAD annotation processor will generate two concrete implementations of your model. For a model MyModel, it would generate:

MyModelImpl

A default concrete implementation of your model.

MyModelWrapper

A wrapper class that will "wrap" another type of entity, and allow you to use the MyModel API with it.

You can use the MyModelImpl class to create new instances of your model:

MyModel model = new MyModelImpl();
model.setName("Steve");

Wrapping Entity with Your Model API

In order to support loose coupling, the annotation processor will generate a "wrapper" class for your model. This class includes a static wrap() method that will "wrap" an existing entity so that it can be used as if it were an instance of your model. This requires that the entities are compatible.

Entity someEntity = ...;

// Wrap someEntity as a MyModel.
MyModel model = MyModelWrapper.wrap(someEntity);
model.setName("Steve");

someEntity.getText(Person.name); // "Steve"

View Models

CodeRAD views automatically generate a view model using properties defined using <define-tag> tags.

For example, given the view defined in "LoginPage.xml" with the following contents:

<?xml version="1.0" ?>
<y>
    <define-tag name="username"/>
    <define-tag name="password"/>

    <label>Enter Username:</label>
    <radTextField tag="username"/>

    <label>Enter Password</label>
    <radTextField tag="password" component.constraint="TextArea.PASSWORD"/>

</y>

This will generate a model named LoginPageModel in the same package as the view. This class will be used as the view’s view model.

See Also

Entities, Properties, and Schemas

A section from the CodeRAD developers guide introductions.

Models

The Models section of the Getting Started tutorial in the CodeRAD developers guide.

Entity javadocs

The javadoc entry for Entity.

Clone this wiki locally