Last active
January 30, 2025 08:51
-
-
Save limkhashing/f319cde8fb4640839ef2c1fe836ad3d9 to your computer and use it in GitHub Desktop.
Dagger-Hilt vs. Koin vs. Manual Dependency Injection
This file contains hidden or 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
// Hilt | |
@Module | |
@InstallIn(SingletonComponent::class) | |
object AppModule { | |
@Provides | |
@Singleton | |
fun provideDatabase(@ApplicationContext context: Context): MyAppDatabase { | |
return MyAppDatabase(context) | |
} | |
} |
This file contains hidden or 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
// Hilt | |
@HiltAndroidApp | |
class HiltApplication : Application() |
This file contains hidden or 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
// Koin | |
class KoinApplication : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
startKoin { | |
androidContext(this@KoinApplication) | |
modules( | |
appModule, | |
viewModelModule | |
) | |
} | |
} | |
} |
This file contains hidden or 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
// Koin | |
val appModule = module { | |
single { MyAppDatabase(androidContext()) } | |
} | |
val viewModelModule = module { | |
factoryOf(::MyRepository) | |
viewModelOf(::MyViewModel) | |
} |
This file contains hidden or 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
class MainActivity: ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
enableEdgeToEdge() | |
setContent { | |
val hiltViewModel hiltViewModel<MyViewModel>() | |
val koinViewModel koinViewModel<MyViewModel>() | |
val manualViewModel = viewModel<MyViewModel>( | |
factory = viewModelFactory { | |
MyViewModel( | |
database = ManualApplication.appModule.database, | |
repository ManualApplication.appModule.repository | |
) | |
} | |
) | |
val state manualViewModel.state | |
... | |
Theme { | |
Scaffold(....) { | |
.... | |
} | |
} | |
} | |
} | |
} | |
This file contains hidden or 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
class ManualApplication: Application() { | |
companion object { | |
lateinit var appModule: AppModule | |
} | |
override fun onCreate() { | |
super.onCreate() | |
appModule = AppModuleImpl(this) | |
} | |
} |
This file contains hidden or 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
// Manual Dependency Injection approach | |
interface ManualAppModule { | |
val database: MyAppDatabase | |
val repository: MyRepository | |
} | |
class ManualAppModuleImpl( | |
private val appContext: Context | |
): AppModule { | |
override val database: MyAppDatabase by lazy { | |
MyAppDatabase(appContext) | |
} | |
override val authApi: AuthApi by lazy { | |
Retrofit.Builder() | |
.baseUrl("https://my-url.com") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
.create() | |
} | |
override val authRepository: AuthRepository by lazy { | |
AuthRepositoryImpl(authApi) | |
} | |
} |
This file contains hidden or 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
@HiltViewModel // Use this annoication only for Hilt | |
class MyViewModel @Inject constructor( | |
private val database: MyAppDatabase, | |
private val repository: MyRepository | |
): ViewModel() { | |
var state by mutableStateOf(value: "database not synced") | |
private set | |
fun syncDatabase() { | |
val data repository.fetchData() | |
database.addData(data) | |
state = "database synced!" | |
} | |
} |
This file contains hidden or 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
// Manual Dependency Injection approach | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.ViewModelProvider | |
fun <VM: ViewModel> viewModelFactory(initializer: () -> VM): ViewModelProvider.Factory { | |
return object : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
return initializer() as T | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=_qb87PN7jlI
DI is not a tool, its a concept
ViewModelFactory
, no need to invokeConstructor
all aroundHilt
Koin
Manual DI