문제

I'm currently trying to use Play! Framework 2.2 and play-slick (master branch). In the play-slick code I would like to override driver definition in order to add the Oracle Driver (I'm using slick-extension). In the Config.Scala of play-slick I just saw /** Extend this to add driver or change driver mapping */ ...

I'm coming from far far away (currently reading Programming In Scala) so there's a lot to learn. So my questions are :

  1. Can someone explain me how to extend this Config object ? this object is used in others classes ... Is the cake apttern useful here ?
  2. Talking about cake pattern, I read the computer-database example provided by play-slick. This sample uses the cake pattern and import play.api.db.slick.Config.driver.simple._ If I'm using Oracle driver I cannot use this import, am I wrong ? How can I use the cake pattern to define an implicit session ?

Thanks a lot. Waiting for your advices and I'm still studying the play-slick code at home :)

도움이 되었습니까?

해결책

  1. To extend the Config trait I do not think the cake pattern is required. You should be able to create your Config object like this:

    import scala.slick.driver.ExtendedDriver
    
    object MyExtendedConfig extends play.api.db.slick.Config {
       override def driverByName: String => Option[ExtendedDriver] = {name: String => 
         super.driverByName(name) orElse Map("oracledriverstring" -> OracleDriver).get(name)
       } 
    
       lazy val app = play.api.Play.current
       lazy val driver: ExtendedDriver = driver()(app)
    }
    

    To be able to use it you only need to do: import MyExtendedConfig.driver._ instead of import play.slick.db.api.Config.driver._. BTW, I see that the type of the driverByName could have been a Map instead of a Function making it easier to extend. This shouldn't break though, but it would be easier to do it.

  2. I think Jonas Bonér's old blog is a great place to read what the cake pattern is (http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/). My naive understanding of it is that you have a cake pattern when you have layers that uses the self types:

    trait FooComponent{ driver: ExtendedDriver =>
      import driver.simple._
      class Foo extends Table[Int]("") {
        //...
      }
    }
    

    There are 2 use cases for the cake pattern in slick/play-slick: 1) if you have tables that references other tables (as in the computer database sample) 2) to have control over exactly which database is used at which time or if you use many many different types. By using the Config you do not really need the cake pattern as long as you only have 2 different DBs (one for prod and one for test), which is the point of the Config.

Hope this answers your questions and good luck on reading Programming in Scala (loved that book :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top