Skip to content

Instantly share code, notes, and snippets.

@mushtaq
Last active December 9, 2015 23:58
Show Gist options
  • Save mushtaq/4347347 to your computer and use it in GitHub Desktop.
Save mushtaq/4347347 to your computer and use it in GitHub Desktop.
ScalaComponents
package demos
import org.specs2.mutable._
class ScalaComponentsUseExtends extends Specification {
trait DbComp {
class Db {
def name = "prod-db"
}
lazy val db = new Db
}
//////////
trait CollectionComp extends DbComp {
class Collection {
def name = s"collection in ${db.name}"
}
lazy val collection = new Collection
}
//////////
trait Collection2Comp extends DbComp {
class Collection2 {
def name = s"collection2 in ${db.name}"
}
lazy val collection2 = new Collection2
}
//////////
trait ServiceComp extends CollectionComp {
class Service {
def name = s"service of ${collection.name}"
}
lazy val service = new Service
}
//////////
trait Service2Comp extends Collection2Comp {
class Service2 {
def name = s"service2 of ${collection2.name}"
}
lazy val service2 = new Service2
}
//////////
trait ControllerComp extends ServiceComp with Service2Comp {
class Controller {
def name = s"controller with ${service.name} and ${service2.name}"
}
lazy val controller = new Controller
}
//////////
"DI" should {
"autowire" in {
object Assembly extends ControllerComp
Assembly.controller.name mustEqual("controller with service of collection in prod-db and service2 of collection2 in prod-db")
}
"autowire root level changes" in {
object Assembly extends ControllerComp {
override lazy val db = new Db {
override def name = "stub-db"
}
}
Assembly.controller.name mustEqual("controller with service of collection in stub-db and service2 of collection2 in stub-db")
}
}
}
package demos
import org.specs2.mutable._
class ScalaComponentsUseSelfType extends Specification {
trait DbComp {
class Db {
def name = "prod-db"
}
lazy val db = new Db
}
//////////
trait CollectionComp {self: DbComp =>
class Collection {
def name = s"collection in ${db.name}"
}
lazy val collection = new Collection
}
//////////
trait Collection2Comp {self: DbComp =>
class Collection2 {
def name = s"collection2 in ${db.name}"
}
lazy val collection2 = new Collection2
}
//////////
trait ServiceComp {self: CollectionComp =>
class Service {
def name = s"service of ${collection.name}"
}
lazy val service = new Service
}
//////////
trait Service2Comp {self: Collection2Comp =>
class Service2 {
def name = s"service2 of ${collection2.name}"
}
lazy val service2 = new Service2
}
//////////
trait ControllerComp {self: ServiceComp with Service2Comp =>
class Controller {
def name = s"controller with ${service.name} and ${service2.name}"
}
lazy val controller = new Controller
}
//////////
trait Assembly extends ControllerComp with ServiceComp with Service2Comp with CollectionComp with Collection2Comp with DbComp
"DI" should {
"autowire" in {
object Assembly extends Assembly
Assembly.controller.name mustEqual("controller with service of collection in prod-db and service2 of collection2 in prod-db")
}
"autowire root level changes" in {
object Assembly extends Assembly {
override lazy val db = new Db {
override def name = "stub-db"
}
}
Assembly.controller.name mustEqual("controller with service of collection in stub-db and service2 of collection2 in stub-db")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment