Skip to content

Molecule object output and dynamic molecules

Compare
Choose a tag to compare
@marcgrue marcgrue released this 04 Mar 14:31
· 248 commits to master since this release

Return each data row as an object with named properties.

As an alternative to tuple output, object properties make it easy to map molecules to forms and other larger structures that benefit from being able to navigate many attributes/properties by name rather than tuple arity position (_1, _2 etc). Property names and types are inferred by the IDE even for references to related data that can nest up to 7 levels deep.

val ben = Person.name_("Ben").age.gender.Address.street.City.name.getObj

ben.age === 23
ben.gender === "male"
ben.Address.street === "Broadway"
ben.Address.City.name === "New York"

Fetch multiple objects with getObjList:

Person.name.age.Address.street.City.name.getObjList.foreach { person =>
  println(s"${person.name} is ${person.age} yeas old and lives on ${person.Address.street}, ${person.Address.City.name}")
}

Dynamic molecules

An exotic new feature that could help domain modelling is the ability to add a code body to a molecule! A Molecule macro makes data of a single molecule object available to a locally defined body of code. And Scala's Dynamic feature makes the code dynamically available from the outside:

val ben = m(Person.name("Ben").age.gender.Address.street.City.name.getObj) { self =>
  def info = s"${self.name} (${self.age}, ${self.gender}) lives on ${self.Address.street}, ${self.Address.City.name}" 
}

// We can now both access data and call body code
ben.age === 23
ben.gender === "male"
ben.Address.street === "Broadway"
ben.Address.City.name === "New York"
ben.info === "Ben (23, male) lives on Broadway, New York"

Dynamic molecules offer combined data and functionality as an alternative to populating more rigid external domain case classes.