hienlt0610
8/26/2019 - 10:52 AM

Clean Architechture Flow

OK let's start with the simple usecase of "Fetch User Info", business requirements specify that you should try to fetch fresh data from an API, if that's not possible, fetch the data from your cache if any.

  • The presentation layer will normally execute the FetchUserInfoUseCase and wait to receive a UserInfo data class, now we've entered the data layer.
  • The FetchUserInfo usecase will use the UserRepository to request this data entity.
  • The UserRepository will try using a UserRemoteDataSource and on failure will fallback to using a UserLocalDataSource.
  • Both UserRemoteDataSource and the UserLocalDataSource return a UserInfo data class.
  • Now getting to the real implementations of the local and remote sources, entering the domain layer, in real-life they could be Retrofit for remote and maybe Room for local.
  • Let's say the remote data source will fetch the data using Retrofit which will mostly return a POJO Gson object as a domain entity, this will need to be mapped to a UserInfo data entity, for that we will need a mapper.
  • Same goes for the local data source, in Room's case, it is a database domain entity which will also need to mapped to our UserInfo data class, so, another mapper.
  • So far both data sources will return the unified data entity UserInfo and will report it back to the data layer's UserRepository which in turn reports back to the FetchUserInfoUseCase which will report back to the presentation layer.
  • Then the presentation layer could map that UserInfo data entity class into a simpler UI version to pass to the view, say a UserInfoViewModel presentation entity.

Hope that clears things up a little.