Sharing Repository and Use Case Layers
Move business logic and data access to commonMain for maximum reuse.
Architecture Goal
In KMP, the goal is to push as much logic as possible into commonMain: domain models, repository interfaces, use cases, and ViewModels. Only platform I/O stays in platform source sets.
// Target architecture:
// commonMain:
// domain/ - data classes, interfaces
// data/ - repository implementations using shared clients
// usecase/ - business logic, composing repositories
// androidMain / iosMain:
// DI wiring, platform drivers (DB, network)Domain Layer in commonMain
Define domain entities and repository interfaces in commonMain. No platform imports.
// commonMain/domain/User.kt
data class User(val id: String, val name: String, val email: String)
// commonMain/domain/UserRepository.kt
interface UserRepository {
suspend fun getUser(id: String): User?
suspend fun getAllUsers(): List<User>
suspend fun saveUser(user: User)
}All lessons in this course
- KMP Project Structure: commonMain, androidMain, iosMain
- expect/actual Mechanism for Platform APIs
- Sharing Repository and Use Case Layers
- Dependency Injection in KMP with Koin