user2270883
user2270883

Reputation: 133

Common Case Class Variables

Does anyone have any thoughts/better way to handle common fields that cross multiple case classes...for example I have the following case class;

case class Customer(
    name: String,
    refId: String = ReferenceIdGenerator.generateRefId("CUSTOMER"),
    createdAt: DateTime = DateTime.now,
    updatedAt: DateTime = DateTime.now,
    id: Option[Long] = None)

The last four variables are repeated in every "model/domain" case class in my example application. Is there a better way to remove that common boilerplate code?

One thought would be to create a "common" case class and at least the boilerplate code is limited, for example;

case class CommonFields(refId: String...etc)
case class Customer(name: String, common: CommonFields)

Thoughts?

Upvotes: 1

Views: 123

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170723

One thought would be to create a "common" case class and at least the boilerplate code is limited, for example

Yes, this is the correct solution, assuming this works well with whatever you use to interact with the database. If you are using Slick, see also Extending SLICK Tables in a DRY manner.

Upvotes: 4

Related Questions