longevity

A Persistence Framework for Scala and NoSQL

View project on GitHub

controlled vocabularies

There are two major approaches to doing controlled vocabularies in Scala: using scala.Enumeration, or using a sealed trait with case objects. Here’s a controlled vocabulary for account status that uses the latter approach:

sealed trait AccountStatus
case object Active extends AccountStatus
case object Suspended extends AccountStatus
case object Cancelled extends AccountStatus

Because longevity supports case objects and polymorphism, this pattern directly translates into a longevity domain model. We are free to use this controlled vocabulary in our domain, such as:

import longevity.model.annotations.persistent

@persistent(keySet = emptyKeySet)
case class Account(
  name: String,
  accountStatus: AccountStatus)

We just need to annotate the members of our controlled vocabulary with @polyComponent and @derivedComponent:

import longevity.model.annotations.polyComponent
import longevity.model.annotations.derivedComponent

@polyComponent
sealed trait AccountStatus

@derivedComponent[AccountStatus]
case object Active extends AccountStatus

@derivedComponent[AccountStatus]
case object Suspended extends AccountStatus

@derivedComponent[AccountStatus]
case object Cancelled extends AccountStatus

Support for scala.Enumeration would be a great addition, and is on our story board. If you are interested, this would be a great user contribution, and we would be happy to support you in implementing it however we can.

prev: polymorphic persistents
up: subtype polymorphism
next: repositories