Model Code
Let’s learn how the model of the application is coded.
Code each class of the JS class model
For instance, the Publisher
class from the JS class model is coded in the following way:
class Publisher {constructor({ name, address }) {this.name = name;this.address = address;// derived inverse reference property (inverse of Book::publisher)this._publishedBooks = {}; // initialize as an empty map}get name() {... }static checkName(n) {... }static checkNameAsId(n) {... }static checkNameAsIdRef(n) {... }set name(n) {... }get address() {... }static checkAddress(a) {... }set address(a) {... }toString() {... }toRecord() {... }}
We add the derived multivalued reference property publishedBooks
, but we don’t assign it in the constructor function. This is because it’s a read-only property that’s assigned implicitly when its inverse leader reference property Book::publisher
is assigned.
Code the set
methods of single-valued properties
Any setter for a reference property that’s coupled to a derived inverse reference property also needs to assign, add, or remove inverse references to and from the corresponding collection value of the inverse reference property. An example of such a setter is publisher
in the Book
class:
set publisher(p) {if (!p) { // the publisher reference is to be deleted// delete the inverse reference in Publisher::publishedBooksdelete this._publisher.publishedBooks[this._isbn];// unset the publisher propertydelete this._publisher;} else {// p can be an ID reference or an object referenceconst publisher_id = (typeof p !== "object") ? p : p.name;const constraintViolation = Book.checkPublisher(publisher_id);if (constraintViolation instanceof NoConstraintViolation) {if (this._publisher) {// delete the inverse reference in Publisher::publishedBooksdelete this._publisher.publishedBooks[this._isbn];}// create the new publisher referencethis._publisher = Publisher.instances[publisher_id];// add the new inverse reference to Publisher::publishedBooksthis._publisher.publishedBooks[this._isbn] = this;} else {throw constraintViolation;}}}
For any multivalued reference property that’s coupled to a ...