Junior — Middle
How can the business logic layer be connected to the data layer in the system architecture?
sobes.tech AI
Answer from AI
The connection between the business logic layer (e.g., Use Cases or Interactors) and the data layer (repositories) in architecture is usually implemented through abstractions and interfaces.
- The business logic layer depends on repository interfaces, not on specific implementations.
- Repositories encapsulate data access (DB, network, cache).
- Dependency injection allows passing specific repository implementations into the business logic.
Example in Kotlin for Android:
interface UserRepository {
fun getUser(id: String): User
}
class UserUseCase(private val userRepository: UserRepository) {
fun execute(id: String): User {
return userRepository.getUser(id)
}
}
// Dependency injection
val userRepository: UserRepository = UserRepositoryImpl()
val userUseCase = UserUseCase(userRepository)
This approach ensures loose coupling, simplifies testing, and allows replacing data sources without changing the business logic.