felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.79k stars 3.39k forks source link

Where to put logic which is neccessary in data layer? Question about project architecture #1267

Closed tmaihoff closed 4 years ago

tmaihoff commented 4 years ago

Hey Felix,

first of all thank you so much for this package, I really love it.

I'm working on a project at the moment which is growing quite big and I'm not quite sure with the overall architecure when it comes to certain things.

I use a UI layer, a Bloc layer and a data layer. The Bloc layer should handle the logic of the application, while the data layer is resposible for fetching and storing data through repositories and providers.

While the bloc handles the logic that is neccessary between ui inputs and data requests, where do I put the logic that happens while processing data?

In my concrete example the user is able to vote the content of the displayed listtiles up or down, which is then saved in a firestore database. So when pressing the vote button the event is sent to the bloc which then calls the repository to process the vote by calling the database. When the phone is offline, the vote is queued and uploaded to the database once the device gets online again. This, however, requires some logic, like checking for internet connection, saving the queued votes somewhere and accessing them once getting online again.

Where would you put this logic? Inside the repository, so that the bloc just sends the vote request to the repository? Or put the logic inside the bloc which then calls the repository or the requests to queue the vote depending on the internet connection?

Or would it even be possible to make a complete new bloc which takes care of queueing and uploading queued votes? - This bloc wouldn't have a connected ui though.

Is there a best practice? I studies the example projects on the flutter bloc site, however they seem to be rather simple and didn't answer my question.

I'm really looking for reading your answer.

All the best, Tobias

narcodico commented 4 years ago

Hi @Tiebo πŸ‘‹

You don't have to worry about this, since firestore caches data locally. As long as your write is not made within a transaction, your data will be synced up automatically once there's an internet connection. πŸ‘

tmaihoff commented 4 years ago

Thank you for your answer @RollyPeres. I know that firestore is capable of this, however I'm not gonna use this feature. Furthermore, the question adresses the general problem of architecture and where to put such kind of logic.

Nevertheless, thanks for your suggestion :)

narcodico commented 4 years ago

I don't have enough information about your requirements to give you the right answer. One way would be to expose a connectivity stream through a repository and listen to it in the bloc that cares about offline data. Keep the votes on the bloc state and remove them once they are saved. Make the bloc hydrated so you can reprocess votes that weren't saved upon recreation. Listen to that stream and when internet is back just add an event that will check whether you have unsaved votes and save them.

felangel commented 4 years ago

Thanks @RollyPeres as always for taking the time to answer!

@Tiebo closing for now but please feel free to comment with additional information or questions and I'm happy to continue the conversation πŸ‘