Kotlin Coroutines use cases introduction
Kotlin Coroutines library was designed to suit our typical backend and Android use cases. That means, that even though every problem can be solved in many ways, for most situations, there are well established patterns that should be preferred. Some of them are expressed by Kotlin Coroutines creators themselves, while others are established by discussions in our community. I collected those use cases, and I would like to present my vision of how Kotlin Coroutines should be used in typical use cases, in typical applications.
I divided those use cases by the layers they are typical to be used in. Those three layer are common both for most modern backend and Android applications:
- Data/Adapters Layer - The layer that is responsible for data storage or interaction with other systems. This layer is mainly made of repositories that each can contain zero to many data sources. In this layer, our coroutines interact with other libraries, including those that might not support them.
- Domain Layer - The layer where our application business logic is implemented. It includes classes encapsulating specific operations, like use cases, services, facades, etc. In this layer, coroutines are used to optimize our application core processes.
- Presentation/API/UI layer - The layer that starts our application's processes. It might be considered an entry to our application. In this layer, we start coroutines and handle their execution results.
I hope you will have fun reading about those use cases. If you disagree with any of them, or if you see some important use case that is missing, let me know via the email firstname.lastname@example.org.