I'm trying to use the lifted embedding approach of Slick on a real-life case (self-management of personal data for sports club members). I have already managed to retrieve the information from the database and to update records (implementing Member as a case class and using the case class copy method, as shown below) but I have some hard time finding the best way to implement member modification in a natural way.
I have contemplated 2 possibilities : 1) Maintaining immutability of class instances and implementing a general setter (see code) 2) Making the constructor parameters "var"'s (what would suppress immutability, and therefore not ideal)
Sticking with option 1, I came up with the following code (excerpt, not the whole source) :
case class Member(id: Int, name: String, firstname: Option[String] = None,
birthDate: Option[Date] = None, gender: Option[String] = None, country: Option[String] = None,
healthNotes: Option[String]) {
// Just a try so far
def set(n: String = name, fn: Option[String] = firstname, bd : Option[Date] = birthDate)
(implicit session: Session) = {
val m = this.copy(id,n,fn,bd)
Members.update(m)
m
}
}
object Members extends Table[Member]("clubmembers")
with CayenneAutoPKSupport {
def id = column[Int]("Member_ID", O.PrimaryKey) // This is the primary key column
def name = column[String]("Name")
def firstname = column[Option[String]]("Firstname")
def birthDate = column[Option[Date]]("BirthDate")
def gender = column[Option[String]]("Gender")
def country = column[Option[String]]("Country")
def healthNotes = column[Option[String]]("HealthNotes")
// Every table needs a * projection with the same type as the table's type parameter
def * = id ~ name ~ firstname ~ birthDate ~ gender ~ country ~ healthNotes <> (Member.apply _, Member.unapply _)
}
This works as intended but I would like to have the same names for the named parameters of the set method (what would made the invocation more "natural"). I tried the following (to no avail)
def set( name: String = this.name, …
This does not compile and I can imagine why the compiler is not happy (OTOH the current implementation seems to work) but I could also imagine that it could work. Anyway: does someone see a way to achieve this?
Alternatively, what would one recommend as best practices to implement modifiable class instances for Slick-persisted objects?
Thanks in advance for any hint.
Regards