Created
November 12, 2024 15:49
-
-
Save anitaa1990/cf627f8ceb51a47459b2162f0b541431 to your computer and use it in GitHub Desktop.
This file contains 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
// Data Layer: Example of combining data sources | |
class UserRepositoryImpl( | |
private val apiService: ApiService, | |
private val userDao: UserDao | |
) : UserRepository { | |
override suspend fun getUserById(userId: String): User { | |
return try { | |
// Fetch from remote API | |
val userDto = apiService.getUserById(userId) | |
// Save to local database for caching | |
userDao.insertUser(userDto.toUserEntity()) | |
userDto.toUser() // Convert DTO to domain model | |
} catch (exception: Exception) { | |
// Fallback to local database if network fails | |
userDao.getUserById(userId).toDomainModel() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment