refactor(database): disallow injection of raw database types

This commit is contained in:
Harsh Shandilya 2023-08-15 14:49:24 +05:30
parent 5bd37a837c
commit e06a8ba236
No known key found for this signature in database
3 changed files with 24 additions and 3 deletions

View file

@ -26,7 +26,7 @@ object DatabaseModule {
private const val LOBSTERS_DATABASE_NAME = "SavedPosts.db"
@Provides
@[Provides InternalDatabaseApi]
fun provideDatabase(@ForScope(ApplicationScope::class) context: Context): LobstersDatabase {
val driver =
AndroidSqliteDriver(

View file

@ -0,0 +1,19 @@
/*
* Copyright © 2023 Harsh Shandilya.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
package dev.msfjarvis.claw.database.injection
import javax.inject.Qualifier
/**
* Neato workaround for not allowing the [dev.msfjarvis.claw.database.LobstersDatabase] type to be
* used directly by modules that depend on this one, as we prefer them to use the specific types
* from [QueriesModule] instead. A [Qualifier] applied to the
* [dev.msfjarvis.claw.database.LobstersDatabase] type makes all injection sites require it as well,
* and marking the annotation class' visibility as `internal` lets it stay visible to the Java code
* generated by Dagger but inaccessible by the Kotlin code we're writing ourselves.
*/
@Qualifier @Retention(AnnotationRetention.RUNTIME) internal annotation class InternalDatabaseApi

View file

@ -19,12 +19,14 @@ import dev.msfjarvis.claw.database.local.SavedPostQueries
object QueriesModule {
@Provides
fun provideSavedPostsQueries(database: LobstersDatabase): SavedPostQueries {
fun provideSavedPostsQueries(@InternalDatabaseApi database: LobstersDatabase): SavedPostQueries {
return database.savedPostQueries
}
@Provides
fun providePostCommentsQueries(database: LobstersDatabase): PostCommentsQueries {
fun providePostCommentsQueries(
@InternalDatabaseApi database: LobstersDatabase
): PostCommentsQueries {
return database.postCommentsQueries
}
}