-
Notifications
You must be signed in to change notification settings - Fork 0
Define a abstraction layer to use DAO
Devrath edited this page Oct 22, 2023
·
3 revisions
- Define an
interface
class that has methods that return the data in eachabstract method
ofDAO
.
interface AppRepository {
suspend fun addBook(book: Book)
fun getBooks(): Flow<List<BookAndGenre>>
fun getAllReviews(): Flow<List<ReviewAndBook>>
suspend fun removeBook(book:Book)
. . . . . . . . . . . . . . . . . . . .
. . . . . other methods . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
}
- Implement this method in a
class
andoverride the methods of the
interface`. - In the
constructor
of the class have the parameters of theDAO's
. - Since we have used hilt, We need to have a module object that provides the
DAO
instance reference. With this, we can pass theDAO
references in the constructor of the implementation of the repository. - Since we have annotated the
constructor
of the repository implementation with@Inject
, Now we can directly use this class reference asconstructor injection
.
- Constructor injection is used for injecting the repository
class AddBookUseCase @Inject constructor(
@IoDispatcher val dispatcher: CoroutineDispatcher,
private val appRepositoryImpl: AppRepositoryImpl,
) : SuspendUseCase<AddBookAllInputs, Result<Boolean>>(dispatcher) {
override suspend fun execute(parameters: AddBookAllInputs): Result<Boolean> =
suspendCancellableCoroutine { coroutine ->
}
}
- We could have directly injected
DAO
but we have added one more layer of abstraction because its very powerful to combine the results from 2 or more DAO's if needed.