Skip to content
aloiscochard edited this page Sep 6, 2011 · 3 revisions

Integration patterns

Embedded Context

This is the recommended pattern when integrating Sindi, using embedded context is flexible and does not require a global value:
(it's the same pattern used for modules, the only difference here is the usage of non childable context)

import sindi._

trait Application extends Context with <...components...> {
  override val modules = Nil
  override val bindings: Bindings = Nil

}

You can find a complete demo application in examples.

Global Context

WARNING: To be used only inside application, you should NEVER use this pattern if you are creating a library/framework

When you can't have control over components initialization (i.e. singletons), you should use a global context:

import sindi._

object ApplicationContext extends Context {
  override val modules = Nil
  override val bindings = Nil

  trait ApplicationComponent extends ComponentWith[ApplicationContext] { override val context = ApplicationContext }
}

And access it by mixing the ApplicationComponent trait:

import ApplicationContext.ApplicationComponent

trait MyController extends ApplicationComponent with MyComponent

Web Framework

Bowler

The bootstrap class give you full control over controllers initialization, you can use the embedded pattern:

trait MyController extends Controller /* with MyComponent */

class Bootstrap extends Context {
  override val modules = Nil
  override val bindings = Nil

  ...
  val controller = new ComponentContext with MyController
  ...
}

You can find a complete Sindi enabled Bowler application in examples.

Play! Framework

Controllers are singletons in Play!, you should use the global context pattern to provide application dependencies to them:

import play._
import play.mvc._

import sindi._

object Application extends ApplicationContext.Controller /* with MyComponent */ {
  
  import views.Application._
  
  def index = {
    html.index("Your Scala application is ready!")
  }
}

object ApplicationContext extends Context {
  override val modules = Nil
  override val bindings = Nil

  class Controller extends play.mvc.Controller with ApplicationComponent

  trait ApplicationComponent extends ComponentWith[ApplicationContext] { override val context = ApplicationContext }
}

Web Service Container

BlueEyes

Services are mixed directly inside server, this perfectly suit Sindi design and integrating it couldn't be more straightforward:

 trait MyServices extends BlueEyesServiceBuilder /* with MyComponent */ {
   ...
 }

 object MyServer extends BlueEyesServer with Context with MyServices {
  override val modules = Nil
  override val bindings = Nil
 }

You can find a complete Sindi enabled BlueEyes service in examples.

Spray

With full control over services initialization, you can easily integrate Sindi using the embedded pattern:

trait MyService extends Directives /* with MyComponent */

class Boot extends Context {
  override val modules = Nil
  override val bindings = Nil

  ...
  val service = new ComponentContext(this) with MyService
  ...
}

Others

Framework Pattern
Scalatra embedded
Lift Web Framework global