The data conversion cycle in Clean Architecture is not fully clear for me.
Datasource receives raw data from local/external source, parses it and returns a DTO.
Repository uses Mapper to create domain models from that data and returns to UseCase.
But what type of data UseCase returns to View ?
If domain models contain some complex logic - business rules checks, some methots et cetera - View doesnt need that. Storing an array of that objects is unnecessary performance waste.
But if view will convert that data to it's own models - it would hit the performance too cause we must iterate through an array of domain models and convert them to array of view models. And before that we converted data models to domain models. Maybe i dont understand anything )
For example i execute GetAllPostsUseCase.
I receive an array of 100 Posts. I have Array<IPostResponseDTO> on that step.
Create domain models from it to check validity. Here i have Either<Failure, Array<Post>>. Or just Array<Post> for simplicity.
For example Post model have some validation rules and a "business rule" method addLike which increments likes count only if it's less than 100 (for example). And View doesnt need that logic. It's used in AddLikeToPostUseCase for example.
Then UseCase returns that Array to ViewModel ? And it stores it as is in some observable array of data which passes to View ? Or converts to View layer models and stores ?
Also where did the data merge takes place ?
For example i have... CosmeticShops which has the brandsIds array. I receive CosmeticShops from ShopsRepository and Brands from BrandsRepository. And in View i must show a ShopsList of shops where user can press a button on each ShopCard to see a list of brands selled in this shop. Where it would be best to merge data for that ShopsList to use ?
And i would appreciate if someone will explain the reverse data flow also.
As i understand that:
In ViewModel i have some observable array of posts e.g. js mobx observable, kotlin mutablelivedata, flutter bloc state etc. (If i understand ViewModel right)
For example i execute method addLike(postId) of ViewModel.
This method executes await this.addLikeToPostUseCase.execute(postId)
That usecase will call const post = await this.postsRepository.getPostById(postId), then call it's method post.addLike() and depending on result save that post in repo and return success or return failure.
Then here i see two variants:
I can either increment likes count for that post in ViewModel data and it wil render in View. Than execute usecase and if everything is ok than do nothing or show error toast or snackbar and revert changes in ViewModel data in case of failure.
Or actually show a loader, execute UseCase first and then increment likes in view or show some error.
Am i right with that data flow ?