playframework - Scala Play 2.4.x controller class chaining? -
i'm using play-slick-3.0 project base migrating legacy play project have. legacy controller.application
used object , class, likewise other controller implementations objects , classes. legacy project doing "controller chaining" i.e. application delegates other controllers:
class application extends controller { ... def uploaddo(context: string) = { implicit request ⇒ // lot of boilerplate code common contexts context match { case "aum" ⇒ aumcontroller.uploaddo(storedfile) case "portfolio" ⇒ portfoliocontroller.uploaddo(storedfile) case "price" ⇒ pricecontroller.uploaddo(storedfile) } // more boilerplate code common contexts } }
the problem can no longer kind of delegation because aumcontroller class , not object anymore. can't define associated object because these classes instantiated , dependencies injected framework ... see instance application.scala in same project, not possible instantiate directly.
how can fix/migrate delegation issue above?
you can make use of reverse routing strategy
class application extends controller { ... def uploaddo(context: string) = { implicit request ⇒ // lot of boilerplate code common contexts context match { case "aum" ⇒ routes.aumcontroller.uploaddo(storedfile) case "portfolio" ⇒ routes.portfoliocontroller.uploaddo(storedfile) case "price" ⇒ routespricecontroller.uploaddo(storedfile) } // more boilerplate code common contexts } }
Comments
Post a Comment