Permalink
Browse files

Advanced demonstration example

  • Loading branch information...
1 parent 32a5542 commit 5f84d3a118b2bfd74f9ff3ed98e5b57aae0eb6c3 @aloiscochard committed Aug 16, 2011
@@ -1,14 +1,14 @@
-name := "examples.basic"
+name := "demo"
-version := "0.2"
+version := "0.2-SNAPSHOT"
-organization := "org.scala-tools.sindi"
+organization := "org.scala-tools.sindi.examples"
scalaVersion := "2.9.0-1"
scalacOptions += "-unchecked"
-crossScalaVersions := Seq("2.9.0-1", "2.9.1.RC1")
+crossScalaVersions := Seq("2.9.0-1", "2.9.1.RC2")
libraryDependencies += "org.scala-tools.sindi" %% "sindi" % "0.2-SNAPSHOT"
@@ -1,7 +1,8 @@
-package sindi.examples.basic
+package sindi.examples.demo
import sindi._
+/*
/////////////////
// Application //
/////////////////
@@ -81,3 +82,4 @@ package store {
trait User extends Store
trait UserPreference extends Store
}
+*/
@@ -0,0 +1,18 @@
+package sindi.examples.demo
+
+import sindi._
+
+import dao._
+
+object Application extends App with Context with TaskComponent with UserComponent {
+ implicit val context = this
+ override val modules = (new DaoModule) :: Nil
+
+ /*
+ override val bindings: Bindings = bind[store.User] to user
+
+ private lazy val user = new store.User with store.RemoteStore
+ */
+
+ println(tasks.get)
+}
@@ -0,0 +1,19 @@
+package sindi.examples.demo
+package dao
+
+import sindi._
+
+import store._
+
+class DaoModule(implicit val context: Context) extends Module {
+ override val modules = StoreModule.of[Task](this) :: StoreModule.of[User](this) :: Nil
+}
+
+trait TaskComponent extends Component {
+ val tasks = from[StoreModule[Task]].inject[Store[Task]]
+}
+
+trait UserComponent extends Component {
+ val users = from[StoreModule[User]].inject[Store[User]]
+}
+
@@ -0,0 +1,5 @@
+package sindi.examples.demo
+package dao
+
+case class Task(title: String, description: String)
+case class User(login: String, name: String)
@@ -0,0 +1,19 @@
+package sindi.examples.demo
+package store
+
+trait Store[T <: AnyRef] {
+ def load(entity: T): T
+ def get: Option[T]
+}
+
+trait MemoryStore[T <: AnyRef] extends Store[T] {
+ var entity: Option[T] = None
+ override def load(e: T): T = { println("MemoryStore.load(" + e + ")"); e }
+ override def get: Option[T] = entity
+}
+
+trait DiskStore[T <: AnyRef] extends Store[T] {
+ var entity: Option[T] = None
+ override def load(e: T): T = { println("DiskStore.load(" + e + ")"); e }
+ override def get: Option[T] = entity
+}
@@ -0,0 +1,19 @@
+package sindi.examples.demo
+package store
+
+import sindi._
+
+object StoreModule {
+ def of[T <: AnyRef : Manifest](context: Context): StoreModule[T] = {
+ implicit val c: Context = context
+ new StoreModule[T]
+ }
+}
+
+class StoreModule[T <: AnyRef](implicit val context: Context, val manifest: Manifest[T]) extends Module {
+ override val bindings: Bindings = bind[Store[T]] to new Store[T] with MemoryStore[T]
+}
+
+abstract class StoreComponent[T <: AnyRef : Manifest] extends Component {
+ lazy val store = from[StoreModule[T]].inject[Store[T]]
+}

0 comments on commit 5f84d3a

Please sign in to comment.