'Are DI frameworks really useful?
there are a sufficient number of DI frameworks in Kotlin's world that pursue one goal - to introduce dependency into the class. More to the point, my question is: why are these frameworks better than just doing this:
// UserService.kt
interface UserService {
fun getUserById(id: String): User
}
// UserServiceImpl.kt
class UserServiceImpl : UserService {
override fun getUserById(id: String): User = TODO()
}
// Components.kt
fun userService(): UserService = UserServiceImpl()
// UserOfUserService.kt
class UserOfUserService {
private val userService = userService()
}
This is the most common example that came to my mind.
I know that DI frameworks offer more features than the example above. But in the term of an application with non-complex dependencies - is it possible to use this approach?
Thank you.
Solution 1:[1]
As Michael noted earlier, DI frameworks can be useful in the case of large applications with many dependencies, since it is much easier to delegate their management to the DI framework than to use even more confusing factories.
I wanted to know if my approach to using the factory method for small applications (or rather, libraries) is correct and got the answer, thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | d1snin |
