Implementing my domain model in scala using case classes I got
abstract class Entity { val _id: Option[BSONObjectID] val version: Option[BSONLong] }
and several case classes that define different objects, such as
case class Person ( _id: Option[BSONObjectID], name: String, version: Option[BSONLong] ) extends Entity
I need a way to set _id and version later from a generic method that works with Entity, because I have to share this behavior with all Entities and not try to write it hundreds of times ;-). I would like to be able
def createID(entity: Entity): Entity = { entity.copy(_id = ..., version = ...) }
... but, of course, this will not compile, since the object does not have a copy method. It is generated for each individual class of the class by the compiler ...
What is the best way to achieve this in scala?
So that someone does not ask: I should use case classes, because this is what the third-party library extracts for me from the requests I receive, and instances of the case class are what are later serialized to BSON / MongoDB ...
source share