vernac / architecture, expressed.
Browse documentation

Change a native behavior and scenario

Vernac 0.7.0 lets you keep business behavior, validation and executable assertions in the model. Add vernac to the behavior body to opt in. Keep the existing Java-shaped method signature that defines the generated JVM API. Use the keywords from the declared method and its ordered argument names at native call sites.

For example, under src/main/vernac/example/documents/:

namespace example.documents;

id DocumentId;
value DocumentTitle(String);

aggregate Document[DocumentId](mut DocumentTitle) validates {
    require self documentTitle string != "" else EmptyDocumentTitle.
} behavior {
    modify void renameTo(DocumentTitle title) vernac {
        self documentTitle = title.
    }
};

Under src/test/vernac/example/documents/, use the same field paths and comparison expressions in a scenario:

namespace example.documents;

suite DocumentRules {
    scenario rejectsBlankRename {
        given document = Document(DocumentTitle("Minutes"));
        given title = DocumentTitle("Agenda");
        given blank = DocumentTitle("");
        when (document renameTo: title);
        when (document renameTo: blank) rejects EmptyDocumentTitle;
        expect (document documentTitle string == "Agenda");
    }
}

A typed unary chain such as document documentTitle string reads one property at a time. renameTo: sends the method one argument. With multiple arguments, the selector reads like booking moveFrom: previous to: destination. Parenthesize a nested keyword call when it is an argument or operator operand. Use the dedicated decimal rounding, date and typed collection operations when the rule needs them. Consult the calculation examples for exact operator and type spellings.

Run vernac test --warm --scenario example.documents.DocumentRules.rejectsBlankRename from the application root. The scenario proves that a rejected modification retains the previously accepted title. For a Spring usecase with fixtures, see test a usecase with a fixture bean.

Change a contained entity

In an aggregate command, send a modifying message to a required child field or to a local binding of the child. Entity lists and sets support lookup by their declared ID type. Lookup returns an optional. Use orReject: with a rejection code to get the entity or reject the command if the lookup misses.

modify void changeDelivery(StopId id, Units units) vernac {
    let selected = self stops by: id.
    let stop = selected orReject: StopNotOnTour.
    stop changeUnits: units.
}

State rules can read contained entity paths and bind entity elements in collection queries. The expression below reads the staged units during final validation.

require (self stops sumBy: [stop | stop units intValue]) <= 100 else TourCapacityExceeded.

A command can modify only entity types that occur in its aggregate’s containment graph. The runtime checks that the actual instance belongs to the active command. Use the tour scenario in examples/namespace-values to exercise a missing stop, an exceeded capacity and a retry on the same objects.