Clean Architecture is worth it
Tuesday, March 10, 2020Clean Architecture gets hate for being verbose. And yeah its verbose.
But I worked on apps without it. When the app grows, the code becomes spaghetti. ViewModel calls API directly, business logic everywhere, impossible to test.
The layers
Three layers. Thats it.
Presentation: UI and ViewModels. Knows about Android.
Domain: Use cases and entities. Pure Kotlin. No Android imports.
Data: Repositories, API, database. Implements domain interfaces.
UI → ViewModel → UseCase → Repository → API/DB
Use cases
Some people think use cases are useless wrappers. Sometimes they are. But when you need to:
- Combine data from multiple sources
- Add business logic
- Make it testable without mocking everything
Use cases shine.
class GetUserProfileUseCase(
private val userRepo: UserRepository,
private val settingsRepo: SettingsRepository
) {
suspend operator fun invoke(id: String): UserProfile {
val user = userRepo.getUser(id)
val settings = settingsRepo.getSettings(id)
return UserProfile(user, settings)
}
}
ViewModel just calls the use case. Doesnt know where data comes from.
When to use it
Small apps? Probably overkill.
Apps that will grow? Worth it. The upfront cost pays back when you need to change things later.
Our team uses it for all new features now. Code reviews are easier when everyone follows same structure.