4

Consider the following simplified domain model i am currently working on:

case class Product(val id : Long, val name : String, product : Option[Product], category : Option[ProductCategory])

case class Price(val id : Long, val amount : Double, val conditions : Seq[Condition])

trait Condition {
  def isTrue(product: Product):Boolean
}
case class ManufacturerNameCondition(val id : Long, val name : String){...}
case class DateCondition(val id : Long, val validFrom : Date, val validTo : Date){...}

What does this model represent?

It represents a simple price service, which allows to define multiple prices for a product or product category. The final price for a product is determined by checking all its matching prices and their associated conditions. E.g. there may be a normal price for a Product and one extra price, which is only active today, because it has a DateCondition.

What is my problem? A Price may be associated with multiple Conditions. Each condition may be of another type. So i am asking myself how this should be solved with Slick (=polymorphic relationships). Basically i want to have a DAO-like interface which fetches the prices along with the associated conditions.

My Questions:

  1. Is it possible to implement this model with Slick? (probably yes)
  2. Polymorphic Relationships: Is it a good idea to solve this problem in Slick? Slick states that it wants to overcome the object relational mismatch and here i have the feeling as if i was trying to fight the framework with this model. Does Slick not play well with such an object-oriented approach? Should i choose another approach?

Plz note: Yes, i have no code to show of what i have tried, but right now i need some direction to look for. Currently i am somehow lost :-)

mavilein
  • 11,648
  • 4
  • 43
  • 48
  • Right after posting this questions and having read many other questions, i discovered this question: http://stackoverflow.com/questions/19309183/scala-slick-table-inheritance With those promising presentation links: http://parleys.com/play/51c2e20de4b0d38b54f46243/chapter73/about http://parleys.com/play/51c2e20de4b0d38b54f46243/chapter57/about Probably this is a good starting point for people like me :-) – mavilein Mar 21 '14 at 16:50

1 Answers1

5

Slick does not over-come the object-relational impedance mismatch, it avoids it. The answer how to model it in Slick is basically how to you model it best in a relational model (not an ORM). What Slick add's on top of SQL is type-safety, scala-semantics and composability, i.e. the ability to write functions and code fragments which you can sanely re-use. If you really need inheritance, you will have to map it yourself, but Slick's composability helps you to make it not overly painful. E.g. have a kind or role column, that tells you what it is. Then either do single-table inheritance and provide a function to the <> mapping operator in Slick, that produces the right sublasses. Or do class-table inheritance, join everything and do the same. Or write a function, which you can give a kind and it only does the necessary joins. You may even be able to put it in a library and put it on github for others to re-use :).

cvogt
  • 11,260
  • 30
  • 46