DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. CRUD Options in Scala

CRUD Options in Scala

Jan Machacek user avatar by
Jan Machacek
·
Nov. 14, 12 · Interview
Like (0)
Save
Tweet
Share
5.76K Views

Join the DZone community and get the full member experience.

Join For Free

 Here’s a blog post exploring the different CRUD approaches in your Scala code. I will show you three different approaches, highlighting the advantages and disadvantages of each. The main motivation were our experiments with Casbah-free Scala 2.10 project.

Approach 1

The first approach is to define Crud[T] for some type T and require the MongoSerializer and IdentityQueryBuilder for that T to be mixed in.

trait Crud[T] {
  this: MongoSerializer[T] with IdentityQueryBuilder[T] =>
 
  def collection: DBCollection  

  def updateFirst(entity: T): Option[T] = {
    val id = createIdQuery(entity)
    if (collection.findAndModify(id, serialize(entity)) != null) Some(entity)
    else None
  }

}

This allows us to construct trivial CRUD components:

val userCrud = new Crud[User] 
   with MongoSerializer[User] with IdentityQueryBuilder[User] {

   def collection: DBCollection = 
     // set the collection where the Users will end up
   def serialize: ...
   def deserialize: ...
   def createIdQuery: ...
}

We would like to save our typing and have the MongoSerializer and IdentityQueryBuilder components pre-prepared and possibly generalised to be applicable to different types. The IdentityQueryBuilder would be easy to define, giving us:

trait IdUuidIdentityQueryBuilder[A <: {def id: UUID}] 
  extends IdentityQueryBuilder[A] {
  ...
}

The MongoSerializer would give us more headaches. We would like to use existing JSON serializers, but they inevitably require more type information than we can provide in MongoSerializer[A]. (Easiest way of thinking about this is that the type parameter A would have to be reified**.)

Approach 2

In the second approach, we need access to the full type parameters. This means that our Crud[A] can no longer be a trait. It needs to become an abstract class:

abstract class Crud[A : MongoSerializer : IdentityQueryBuilder] {
  val serializer = implicitly[MongoSerializer[T]]
  val identityQueryBuilder = implicitly[IdentityQueryBuilder[T]]

  def collection: DBCollection  

  def updateFirst(entity: T): Option[T] = {
    val id = identityQueryBuilder.createIdQuery(entity)
    if (collection.findAndModify(id, 
      serializer.serialize(entity)) != null) Some(entity)
    else None
  }

}

This approach does not drop the methods from the self-type inheritance into the methods in Crud[A]. However, it requires unique instance of MongoSerializer and IdentityQueryBuilder for type A to be available in the scope where an instance of Crud[A] is constructed.

implicit def productMongoSerializer[A <: Product] = new MongoSerializer[A] {
  def serialize(entity: A) = ???

  def deserialize(dbObject: DBObject) = ???
}

implicit def UUIDIdIdentityFieldExtractor[A <: {def id: Long}] = 
  new IdentityQueryBuilder[A] { def createIdQuery(entity: A) = ??? }

val crud = new ImplicitCrud[SomeEntity] {
  def collection = db.getCollection("some")
}

The reason for this is that Scala generates the constructor for the above class as

abstract class Crud[A](
  implicit ev1: MongoSerializer[A], ev2: IdentityQueryBuilder[A])

Thus, in effect, reifying the type parameters. The good news is that you can require further typeclasses to be available. For example, it would be quite reasonable to require instance of JsonWriter to be available for our MongoSerializer of A:

implicit def productMongoSerializer[A <: Product : JsonWriter] = 
  new MongoSerializer[A] {
  
  val writer = implicitly[JsonWriter[A]]

  def serialize(entity: A) = ??? 

  def deserialize(dbObject: DBObject) = ???
}

This will give us more freedom, but it means making the serializers / query builders available in the scope where we create instances of the CRUDs. It also means that the CRUD is one unit and that separating out the C from R from U from D would mean breaking the Crud[A] trait.

Approach 3

Which leads us to the final approach, where we leave the Crud as a trait, but make its methods polymorphic, requiring the typeclasses to be available. Doing so frees the Crud from types of the entities it processes. This, however, means that the DBCollection where the entities end up needs to be specified further. As usual, we have several choices:

3.1

The first choice is to simply require the DBCollection to be supplied as parameter.

trait Crud {
  def updateFirst[T : MongoSerializer : IdentityQueryBuilder]
    (collection: DBCollection, entity: T): Option[T] = {
    val serializer = implicitly[MongoSerializer[T]]

    val id = implicitly[IdentityQueryBuilder[T]].createIdQuery(entity)
    if (collection.findAndModify(id, 
      serializer.serialize(entity)) != null) Some(entity)
    else None
  }
}

3.2

In the second choice, we curry all functions of Crud:

trait Crud {
  def updateFirst[T : MongoSerializer : IdentityQueryBuilder]
    (collection: DBCollection)(entity: T): Option[T] = {
    val serializer = implicitly[MongoSerializer[T]]

    val id = implicitly[IdentityQueryBuilder[T]].createIdQuery(entity)
    if (collection.findAndModify(id, 
      serializer.serialize(entity)) != null) Some(entity)
    else None
  }
}

3.3

And the third choice introduces the CollectionProvier[A], which gives the DBCollection depending on the type A.

trait Crud {
  def updateFirst[T : MongoSerializer : IdentityQueryBuilder : 
                      CollectionProvider](entity: T): Option[T] = {
    val collection = implicitly[CollectionProvider[T]].getCollection
    val serializer = implicitly[MongoSerializer[T]]

    val id = implicitly[IdentityQueryBuilder[T]].createIdQuery(entity)
    if (collection.findAndModify(id, 
      serializer.serialize(entity)) != null) Some(entity)
    else None
  }
}

The differences in the code are very subtle, but the effects are quite large.

// assuming the required typecalss instances are in scope

val crud31 = new Crud {}
val crud32 = new Crud {}
val crud33 = new Crud {}
val users: DBCollection = ...
val userUpdateFirst = crud32.updateFirst[User](users)

crud31.updateFirst(users, User(...))
crud32.updateFirst(users)(User(...)) 
/* or */ 
userUpdateFirst(User(...))
crud33.updateFirst(User(...))

With crud31, we need to jump through hoops and specify the collection in every call. This could be error-prone. With crud31, we can apply the updateFirst function to just the first parameter list, giving us a function that performs the update operation on the users collection. This still leaves us with some possibility of errors. Finally, with crud33, the type of the entity drives the collection in which it is going to end up. (This is exactly what we need, except in cases where we’d like to be able to change the collection.)


*** F# makes the distinction much clearer: if you use 'a, you are defining generic element, if you use ^a, you are defining polymorphic element with respect to the type a. And, because the code ends up as CLR code, type-level parameters can only be generic. Just like Scala.








Scala (programming language)

Published at DZone with permission of Jan Machacek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Simulate Network Latency and Packet Drop In Linux
  • Playwright vs. Cypress: The King Is Dead, Long Live the King?
  • Three SQL Keywords in QuestDB for Finding Missing Data
  • How to Cut the Release Inspection Time From 4 Days to 4 Hours

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: