0
votes

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

1
Have you tried Play Slick plugin ? github.com/playframework/play-slicktuxdna
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
Can you share the signature of both the DBAction and AuthAction ?tuxdna
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: github.com/playframework/play-slick/wiki/UsageRoy Lin
You seem to be confusing the Play Session with the slick db Session. They are not the same thing at all.Michael Zajac

1 Answers

1
votes

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!