Rui
10/19/2020, 3:01 PMlateinit var itemService: ItemService
For now this is what I do, in Order.kt file:
lateinit var itemService: ItemService
fun Route.order() {
post<Order> {
init(...)
}
get<Order> {
init(...)
}
}
init(...) {
itemService = ItemService(...)
}
and in Item.kt file
fun Route.item() {
post<Item> {
init(...)
}
get<Item> {
init(...)
}
}
Basically I put those common objects and their initialization method in one of the routing files, and other routing file can have direct access to those objects and methods.
I wonder what is the more proper way of organizing this? Appreciate any kinds of input !Marc Knaup
10/19/2020, 3:07 PMRoute.order()
needs to the function as parameters.
Alternatively you could use a dependency injection framework (and pass that as a function parameter).
You could also write a feature that puts references into the call and makes them available as an extension property to the call.Rui
10/19/2020, 3:13 PMMarc Knaup
10/19/2020, 3:15 PMinit()
in post<Order> { … }
for example and return the ImageService
(and others)?both of them need to use an instance of ItemService classand
those objects in need will only be initialized when one of the routing gets called, with values from http parametersare conflicting. Should they share an instance of not? If they should share one, then the instance shouldn’t depend on request parameters 🤔
Rui
10/19/2020, 3:24 PMMarc Knaup
10/19/2020, 3:25 PMitemService
val imageService = init(…)
Rui
10/19/2020, 3:29 PMval imageService = init(…)
then where should I put the definition of init(...) method?Marc Knaup
10/19/2020, 3:30 PMinit
you can have wherever you want.
if it just initializes an ImageService
it should be somewhere nearby.
It’s difficult to say without knowing more.Rui
10/19/2020, 3:43 PM