0

I am trying to write my own action and pass in a DatabaseSession implicitly. However, at best I can do something like this in my controller.

def index = MyAction { implicit myRequest =>
  implicit val dbss = myRequest.databaseSession
  aClass.someMethod() // requires an implicit DatabaseSession
}

In playframework, you can access the session like thus:

def index = Action { implicit request =>
  val someOption = session.get("something")
  // OR
  aClass.doSomething() // requires an implicit Session
}

Here, as we can see, you could directly access the session, when only the request is passed in as implicit. So where is the session coming from? And how would I be able to pass in my DatabaseSession just like Session? So that I don't have to write:

implicit val dbss = myRequest.databaseSession

I know this is possible, because slick is able to pass in their dbSession implicitly. But I can't seem to work out how they do it either.

https://github.com/playframework/play-slick/blob/master/code/src/main/scala/play/api/db/slick/DBAction.scala

Totally confused! =S

Roy Lin
  • 730
  • 8
  • 17
  • Have you tried Play Slick plugin ? https://github.com/playframework/play-slick – tuxdna Oct 29 '14 at 10:49
  • Yea, I have, but i needed to mix DBAction with my on AuthAction for authentication. So that's why I need to understand this concept to build my own. – Roy Lin Oct 29 '14 at 11:29
  • Can you share the signature of both the DBAction and AuthAction ? – tuxdna Oct 29 '14 at 11:38
  • Sorry, I dont follow what you mean by sharing the signature. Do you mean compose the actions together? It's quite difficult to compose, says the wiki: https://github.com/playframework/play-slick/wiki/Usage – Roy Lin Oct 29 '14 at 11:45
  • You seem to be confusing the Play `Session` with the slick db `Session`. They are not the same thing at all. – Michael Zajac Oct 29 '14 at 22:34
  • Yup, they are not. I was using slick dbSession as an example. Both the slick dbSession and the play's storage session both implicitly exist when you define an Action. I was just wondering how that is possible. – Roy Lin Oct 29 '14 at 23:27

1 Answers1

1

After some hard digging, I found the solution to my own question.

The secret lies in the Controller, which has an implicit def:

implicit def request2session(implicit request: RequestHeader): Session

PlayFramework, you're smart!

Roy Lin
  • 730
  • 8
  • 17