In my web application authorized user has at least 4 “facets”: http session related data, persistent data, facebook data, runtime business data.
I’ve decided to go with case class composition instead of traits for at least two reasons:
- traits mixing can cause name clashes
- i want the free case class goodies like pattern matching and copy method
I’d like to know experienced scalaists opinions on this subject. It looks like traits and/or cake pattern should be suitable for such tasks but as i’ve mentioned above there are problems… Its obvious that not only i want to implement it fast and easy but also to understand it in depth for using in future.
So does my decision have any flaws and misunderstanding or is it right?
Related code looks like this:
case class FacebookUserInfo(name: String, friends: List[Long])
case class HttpUserInfo(sessionId: String, lastInteractionTime: Long, reconnect: Boolean)
case class RuntimeQuizUserInfo(recentScore: Int)
trait UserState {
def db: User
def http: HttpUserInfo
}
case class ConnectingUser(db: User, http: HttpUserInfo) extends UserState
case class DisconnectedUser(db: User, http: HttpUserInfo, facebook: Option[FacebookUserInfo]) extends UserState
case class AuthorizedUser(db: User, http: HttpUserInfo, facebook: FacebookUserInfo,
quiz: RuntimeQuizUserInfo) extends UserState
The third option is to use implicit converters aka “pimp my library,” which probably is not necessary since you have the control of the code.
It all depends on how opaque (or transparent) you want to be about certain aspect of your object. You can pretend its a plain old case class to the rest of the world, but internally make it do extra work by using implicits. The use of case class to hold data is appropriate but I also feel that it’s awkward to represent the same object using three classes (
ConnectingUser,DisconnectedUser,AuthenticatedUser) depending on her state of authentication.For the
UserState, you could provide an extractor so it behaves like a case class:this can be used in a match state as follows: