Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created November 12, 2024 15:49
Show Gist options
  • Save anitaa1990/cf627f8ceb51a47459b2162f0b541431 to your computer and use it in GitHub Desktop.
Save anitaa1990/cf627f8ceb51a47459b2162f0b541431 to your computer and use it in GitHub Desktop.
// 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