liveBook · Manning

Reference relation

As explained in Building User-Friendly DSLs

A directed relation that lets one object refer to another without making the target part of the source object's containment hierarchy.

A reference relation is a directed link from a source object to a target object. Unlike containment, it does not make the target owned by, or structurally inside, the source. In an object-relation diagram, containment arrows are solid and reference arrows are dashed. Ignoring references leaves a containment tree; references add the links needed for general structured data. section 2.1

Figure 2.6 Adding and annotating a reference relation
figure

Use the dashed arrow to distinguish a reference from a containment relation and read its property label as the link stored on the source object. section 2.1

Treat a relation as a setting on its source object: the arrow label is the property name, and the target object is its value. A concept property has one consistent value kind: simple setting, containment relation, or reference relation. Property names must also be unique within a concept. section 2.1

Referencing an attribute in a rental record

An attribute’s initial value can be represented by a separate contained object. When that value is an Attribute Reference, its attribute setting can refer to another Data Attribute, such as using rental price before discount as the initial-value input for rental price after discount. In JavaScript, follow the relation through .ref and verify target identity against the corresponding attribute object. section 2.2 section 3.2.1

References preserve a live connection rather than a copied value: a projection can retrieve a referenced attribute’s name by following settings["attribute"].ref.settings["name"]. Treating every JavaScript object link as deep containment breaks that expectation and can fail on cycles, including a self-reference. section 5.1.6 section 7.4

Persistence must serialize and deserialize reference relations separately from contained objects so an AST can be stored as JSON and transported over HTTP. During observable deserialization, create observable AST objects, record identifiers, and resolve references without recursively converting an already visited target; this also lets MobX preserve reference behavior. section 7.0 section 7.4

How Building User-Friendly DSLs covers it

How deep it goes
Defines notation and constraints, demonstrates JavaScript traversal, then implements serialization and observable deserialization for circular references.
Examples
Uses Rental data attributes, an Attribute Reference initial value, and a self-referential AST that triggers a stack overflow.
What it assumes
Assumes JavaScript objects and arrays while explaining indexed AST navigation and reference access through .ref.
What you can do after
Build it from scratch by traversing, serializing, deserializing, and observably resolving AST reference relations.
How it connects
Connects containment relations, settings, object-relation diagrams, ASTs, serialization, deserialization, MobX, and observability.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.