Created
November 18, 2012 13:06
-
-
Save mushtaq/4105163 to your computer and use it in GitHub Desktop.
Exploring DI options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demos | |
import org.specs2.mutable._ | |
class FPStyleDIDemo extends Specification { | |
class Db { | |
def name = "sony-db" | |
} | |
object Db { | |
implicit def New = new Db | |
} | |
////////// | |
class Collection(implicit db: Db) { | |
def name = s"collection in ${db.name}" | |
} | |
object Collection { | |
implicit def New(implicit d: Db) = new Collection | |
} | |
////////// | |
class Service(implicit collection: Collection) { | |
def name = s"service of ${collection.name}" | |
} | |
object Service { | |
implicit def New(implicit c: Collection) = new Service | |
} | |
////////// | |
class Controller(implicit service: Service) { | |
def name = s"controller with ${service.name}" | |
} | |
"DI" should { | |
"autowire" in { | |
val controller = new Controller | |
controller.name mustEqual("controller with service of collection in sony-db") | |
} | |
"autowire root level changes" in { | |
implicit val stubDb = new Db { | |
override def name = "stub-db" | |
} | |
val controller = new Controller | |
controller.name mustEqual("controller with service of collection in stub-db") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demos | |
import org.specs2.mutable._ | |
import com.google.inject.{AbstractModule, Guice, Inject, Singleton} | |
@Singleton | |
class Db { | |
def name = "sony-db" | |
} | |
@Singleton | |
class Collection @Inject()(db: Db) { | |
def name = s"collection in ${db.name}" | |
} | |
@Singleton | |
class Service @Inject()(collection: Collection) { | |
def name = s"service of ${collection.name}" | |
} | |
@Singleton | |
class Controller @Inject()(service: Service) { | |
def name = s"controller with ${service.name}" | |
} | |
class StubDb extends Db { | |
override def name = "stub-db" | |
} | |
class GuiceStyleDIDemo extends Specification { | |
"DI" should { | |
"autowire" in { | |
val Injector = Guice.createInjector() | |
val controller = Injector.getInstance(classOf[Controller]) | |
controller.name mustEqual("controller with service of collection in sony-db") | |
} | |
"autowire root level changes" in { | |
val mod = new AbstractModule { | |
def configure() { | |
bind(classOf[Db]).to(classOf[StubDb]) | |
} | |
} | |
val Injector = Guice.createInjector(mod) | |
val controller = Injector.getInstance(classOf[Controller]) | |
controller.name mustEqual("controller with service of collection in stub-db") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demos | |
import org.specs2.mutable._ | |
class OOStyleDIDemo extends Specification { | |
trait DbComp { | |
class Db { | |
def name = "sony-db" | |
} | |
val db = new Db | |
} | |
////////// | |
trait CollectionComp extends DbComp { | |
class Collection { | |
def name = s"collection in ${db.name}" | |
} | |
val collection = new Collection | |
} | |
////////// | |
trait ServiceComp extends CollectionComp { | |
class Service { | |
def name = s"service of ${collection.name}" | |
} | |
val service = new Service | |
} | |
////////// | |
class Controller extends ServiceComp { | |
def name = s"controller with ${service.name}" | |
} | |
"DI" should { | |
"autowire" in { | |
val controller = new Controller | |
controller.name mustEqual("controller with service of collection in sony-db") | |
} | |
"autowire root level changes" in { | |
trait StubDbComp extends DbComp { | |
override val db = new Db { | |
override def name = "stub-db" | |
} | |
} | |
val controller = new Controller with StubDbComp | |
controller.name mustEqual("controller with service of collection in stub-db") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment