Skip to content

Instantly share code, notes, and snippets.

@tarsa
Last active June 10, 2018 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarsa/8613e544ee95ec004b0caaf7499e2f9d to your computer and use it in GitHub Desktop.
Save tarsa/8613e544ee95ec004b0caaf7499e2f9d to your computer and use it in GitHub Desktop.
Dependency graph using normal Scala code
// in test source directory
object LocalSetup { // for running app locally
private val localConfig = ???
val instance = new ApplicationSetup(Some(localConfig)).instance
}
// in main source directory
object ProductionSetup extends ApplicationSetup(None)
class ApplicationSetup(
configReplacementOpt: Option[ApplicationConfig] = None
) {
private val config = configReplacementOpt.getOrElse {
??? // here we read config from .props or .conf files or environment variables etc
}
private implicit val actorSystem: ActorSystem = ActorSystem("name", config.akkaConfig)
// here we split out part of dependency graph to separate class that will provide databased to other parts of the system
// NOTE right composition structure depends on a particular case, maybe it would be better to extract modules like
// UsersModule and BlogsModule instead of DatabasesModule and ServerModule
private val dbs = new DatabasesModule(config.dbsConfig)
// userDb and blogsDb have the same type (say: app.Db) so with DI container they would need to be differentiated using
// e.g @Named annotation
private val usersService = new UsersService(dbs.userDb)
private val blogsService = new BlogsService(dbs.blogsDb)
// in DI container case we would need to have some sort of multibinding
private val endpoints = Seq(
new UsersEndpoint(usersService),
new BlogsEndpoint(blogsService)
)
private val httpServer =
new HttpServer(endpoints) // uses implicit ActorSystem, so no need to pass it explicitly
val instance: Application =
new Application(httpServer)
}
// there are no shutdown hooks, but it is relatively easy to add them
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment