Exposing services through plugins

Some plugins need to provide users with helper methods to simplify transactions, whereas others need not do anything besides some tasks to be added in the application's life cycle. For example, our NotifierPlugin just sends e-mails on start and stop. Then, the methods of our CassandraPlugin can be accessed using the plugin method of play.api.Application:

object CassandraHelper {
  private val casPlugin = Play.application.plugin[CassandraPlugin].get

  //complete DB transactions with the connection pool started through the plugin
  def executeStmt(stmt:String) = {
    casPlugin.session.execute(stmt)
  }
  
}

Alternatively, the plugin can also provide a helper object:

object Cassandra { private val casPlugin = Play.application.plugin[CassandraPlugin].get ...

Get Mastering Play Framework for Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.