Last active
June 10, 2018 11:48
-
-
Save tarsa/8613e544ee95ec004b0caaf7499e2f9d to your computer and use it in GitHub Desktop.
Dependency graph using normal Scala code
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
// 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