Reputation: 31
This might be a stupid question but I am relatively new to Scala so please bear with me. I am trying to model a domain object for a Spark job in Scala, which reflects the data structure of the source record and contains more than 100 fields. I am trying to figure out the best way to model this as I don't feel comfortable simply adding all the fields to a single case class. I thought about grouping closely associated fields into nested case classes but then I read in a few places that nesting case classes is not recommended. I would appreciate some input on what would be the best approach.
Edit: In response to Alvaro's comments:
So in essence we are saying that this is not recommended:
case class Product(name: String,
desc: String,
productGroup: String) {
case class ProductPack(packType: String,
packQuantity: Int,
packQuantityUnit: String,
packUnitPrice: Float)
}
While this would be fine:
case class Product(name: String,
desc: String,
productGroup: String,
productPack: ProductPack) {
}
case class ProductPack(packType: String,
packQuantity: Int,
packQuantityUnit: String,
packUnitPrice: Float) {
}
Upvotes: 2
Views: 451
Reputation: 6172
Your update is correct.
Another alternative: If a case class mostly makes sense in the context of another concept, sometimes I define the case class inside a companion to the concept:
case class Product(
name: String,
desc: String,
productGroup: String
productPack: Product.Pack
)
object Product {
case class Pack(
packType: String,
packQuantity: Int,
packQuantityUnit: String,
packUnitPrice: Float
)
}
That should also be fine. The class is contained in an object, but it is not "nested" in the Product
class.
Upvotes: 2