Play Framework: Dependency Inject Action Builder

后端 未结 3 1911
猫巷女王i
猫巷女王i 2020-12-14 19:30

since Play Framework 2.4 there is the possibility to use dependency injection (with Guice).

Before I used objects (for example AuthenticationService) in

3条回答
  •  余生分开走
    2020-12-14 20:28

    I didn't like the way one was required to inherit in the above example. But apparently it's possible to simply wrap a object inside class:

    class Authentication @Inject()(authService: AuthenticationService) {
      object AuthenticatedAction extends ActionBuilder[Request] {
        def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = {
          // Do your thing wit the authService...
          block(request)
        }
      }
    }
    
    class YourController @Inject() (val auth: Authentication) extends Controller (
      def loggedInUser = auth.AuthenticatedAction(parse.json) { implicit request =>
        // ...
      }
    }
    

提交回复
热议问题