SkyWalker
SkyWalker

Reputation: 14309

Scala Play 2.4.x controller class chaining?

I'm using the play-slick-3.0 project as base for migrating a legacy Play project I have. The legacy controller.Application used to be an object and now is a class, likewise all the other controller implementations were objects and now are classes. The legacy project was doing "controller chaining" i.e. Application delegates to other controllers:

class Application extends Controller {
...
  def uploadDo(context: String) = { implicit request ⇒ 
    // a lot of boilerplate code common to all contexts
    context match {
      case "aum" ⇒ AumController.uploadDo(storedFile)
      case "portfolio" ⇒ PortfolioController.uploadDo(storedFile)
      case "price" ⇒ PriceController.uploadDo(storedFile)
    }
    // more boilerplate code common to all contexts
  }
}

The problem is that I can no longer do that kind of delegation because AumController is a class and not an object anymore. I can't also define an associated object because these classes get instantiated and dependencies are injected by the framework ... see for instance the Application.scala in that same project, it is not possible to instantiate it directly.

How can I fix/migrate the delegation issue above?

Upvotes: 1

Views: 195

Answers (1)

Bhavya Latha Bandaru
Bhavya Latha Bandaru

Reputation: 1108

You can probably make use of reverse routing strategy

  class Application extends Controller {
   ...
   def uploadDo(context: String) = { implicit request ⇒ 
    // a lot of boilerplate code common to all contexts
    context match {
      case "aum" ⇒ routes.AumController.uploadDo(storedFile)
      case "portfolio" ⇒ routes.PortfolioController.uploadDo(storedFile)
      case "price" ⇒ routesPriceController.uploadDo(storedFile)
    }
    // more boilerplate code common to all contexts
  }
}

Upvotes: 1

Related Questions