Skip to content

Domain Model Annotation

Alexandre DuBreuil edited this page Jun 11, 2018 · 5 revisions

Installation

See latest version in maven central.

<!-- The dependency for the annotations, field ID, constraints -->
<dependency>
  <groupId>io.doov</groupId>
  <artifactId>doov-core</artifactId>
  <version>LATEST</version>
</dependency>

Sample project

This documentation is based on the sample project in dOOv. You can replace the package name io.doov.sample by your package name as com.example.myapp and classes name Sample with MyApp.

Usage

The idea behind dOOv is that you "key" (annotate with an ID) each field you will want to query later. Those keys are used as a base for the code generator that will produce the DSL.

Path constraint, path annotation and field ID (package field)

You first need to create a path annotation, a field ID, a readable and a path constraint:

  • path annotation make possible the annotation of a field in your model, it contains the field ID, the readable of the field, and the path constraint (optionnal).
  • field ID is the ID of the field, it needs to be unique in the model class (not in the whole model).
  • readable (optionnal) is the human-readable form of the field, is it used in code generation and in the generated abstract syntax tree (if not provided the ID is used).
  • path constraint (optionnal) limits the possible paths to a field from the root of your model, this helps the code generator understand a complex model (if not provided the code generator might stop in ambiguous cases).

Path annotation

package io.doov.sample.field;

@Path
@Retention(RetentionPolicy.RUNTIME)
public @interface SamplePath {

    SampleFieldId field();

    SampleConstraint constraint() default SampleConstraint.NONE;

    String readable() default "";

}

Field ID

package io.doov.sample.field;

public enum SampleFieldId implements FieldId { EMAIL }

Path constraint

package io.doov.sample.field;

public enum SampleConstraint implements PathConstraint { NONE }

Model annotation (package model)

You can then annotate your model with the annotation SamplePath and the field ID EMAIL you created.

Root model class

You need a root model class that will be used as a starting point for the code generator. It is a common pattern to have multiple root class, with multiple code generation, that will generate multiple DSLs.

package io.doov.sample.model;

public class SampleModel {
    
    private Acccount account;
    
    public Acccount getAcccount() {
        return account;
    }

    public void setAcccount(Acccount account) {
        this.account = account;
    }
    
}

Account model class

package io.doov.sample.model;

public class Account {

    @SamplePath(field = SampleFieldId.EMAIL, readable = "account email")
    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

i18n

Operator are already i18n in dOOv. You can also translate your domain model. Instead of using a string in the readable attribute in your path annotation, you can use a key string that will be used to load the corresponding key in the resource bundle.

package io.doov.sample.model;

public class Account {

    @SamplePath(field = SampleFieldId.EMAIL, readable = "account.email")
    private String email;

}

The resource bundle that is automaticaly loaded needs to be located in io.doov.sample.field.i18n, and named SampleModelResourceBundle (because you model root is SampleModel). You can also register more bundle by calling io.doov.core.dsl.meta.i18n.ResourceBundleProvider#register.

account.email=Courriel du compte

Next

See DSL Code Generation.