Helio
01/19/2021, 7:04 AMtynn
01/19/2021, 9:09 AMHelio
01/19/2021, 9:14 AMarnaud.giuliani
01/19/2021, 10:26 AMGlobalContext.get().get()
arnaud.giuliani
01/19/2021, 10:26 AMdefaultContext().get()
tynn
01/19/2021, 10:57 AMKoinComponent
should not be necessary then.arnaud.giuliani
01/19/2021, 10:59 AMHelio
01/19/2021, 10:04 PMget()
.
@tynn, in our scenario, we have a Ktor Controller (which is used to declare our apis) and for this specific case we have this “Helper” class, which handle some Generic Top Level functions for us.
For example, in our Controller, we have the following snippet
. There you can see that we are injecting a UseCase class.
fun Route.tokenApi() {
val createAccessTokenUseCase: CreateAccessTokenUseCase by inject()
route("/token") {
post("/build") {
val buildInfo = call.principal<Principal>().getBuildInfo()
createAccessTokenUseCase.createBuildToken(buildInfo,
TokenatorPermissionGroupProvider.getGroupsFor(call.request.local.uri)).let { call.respond(HttpStatusCode.Created, it) }
}
}
This line val buildInfo = call.principal<Principal>().getBuildInfo()
reference our Top level function getBuildInfo()
.
This function will use different classes depending on the Principal
. The way I was doing before, I was injecting the class I wanted in the Controller context and passing it to my function like this getBuildInfo(injectedClassForPrincipal1)
. Complete example:
fun Route.tokenApi() {
val createAccessTokenUseCase: CreateAccessTokenUseCase by inject()
val myInjectedClassForPrincipalOne: MyInjectedClassForPrincipalOne by inject()
route("/token") {
post("/build") {
val buildInfo = call.principal<Principal>().getBuildInfo(myInjectedClassForPrincipalOne)
createAccessTokenUseCase.createBuildToken(buildInfo,
TokenatorPermissionGroupProvider.getGroupsFor(call.request.local.uri)).let { call.respond(HttpStatusCode.Created, it) }
}
}
Soon, we will need to make getBuildInfo()
able to handle a request for a different principal… Which we would need to add another parameter to getBuildInfo(injectedClassForPrincipal1, injectedClassForPrincipal2)
. Complete example:
fun Route.tokenApi() {
val createAccessTokenUseCase: CreateAccessTokenUseCase by inject()
val myInjectedClassForPrincipalOne: MyInjectedClassForPrincipalOne by inject()
val myInjectedClassForPrincipalTwo: MyInjectedClassForPrincipalTwo by inject()
route("/token") {
post("/build") {
val buildInfo = call.principal<Principal>().getBuildInfo(myInjectedClassForPrincipalOne, myInjectedClassForPrincipalTwo)
createAccessTokenUseCase.createBuildToken(buildInfo,
TokenatorPermissionGroupProvider.getGroupsFor(call.request.local.uri)).let { call.respond(HttpStatusCode.Created, it) }
}
}
Using the GlobalContext.get().get(), doesn’t make necessary for me to be injecting class by class. I can just reference them in the Top level functions it should be used. Also, IMHO, I don’t think for this scenario it makes sense to let my TokenController knows about (myInjectedClassForPrincipalOne, myInjectedClassForPrincipalTwo).
Please, let me know if you think it makes sense or if it doesn’t make sense at all (I’m relative new to Kotlin). Also, I’m always keen to hear different point of views.arnaud.giuliani
01/20/2021, 5:08 PMHelio
01/20/2021, 8:11 PM