felangel / bloc

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

Architecture for repositories within blocs #1827

Closed engineerdeep closed 4 years ago

engineerdeep commented 4 years ago

Hi @felangel 👋 ,

I'm sorry if this question was already asked, I tried to search the same but it wasn't quite answered.

I've created blocs into their own separate packages along with the repository and data provider.

I noticed in your fluttersaurus project (and in many other examples) you're using RepositoryProvider to add a repository to a widget and then use that to inject the repository into the bloc. If the UI is only ever going to interact with the bloc, why do we need to add the repository dependency on the UI? Wouldn't it be better to add the repository as the dependency in the bloc package itself and by doing so the UI will only depend on the bloc package and doesn't need to care about what repository the bloc uses.

I'd love to hear your thoughts for the same & thank you for your time!

rodrigobastosv commented 4 years ago

Hi @engineerdeep ,

Adding the repository dependency on the UI make it simples when you try to test your code, since you can always pass a mock instance of repository to your bloc. Besides that, when trying to text your widgets you can easily mock that too.

engineerdeep commented 4 years ago

Hi @rodrigobastosv,

Thank you for the quick response. Wouldn't mocking the repository be possible when we test the bloc package itself with the repository added there? I don't have much experience with mockito so please pardon me if I'm wrong.

rodrigobastosv commented 4 years ago

Hi @engineerdeep,

If you create the repository instance inside of the bloc it will be much harder to mock this instance.

Passing the repository instance through bloc constructor greatly simplify things, because while testing you can create your mock repository instance and pass it to the bloc you are testing.

I hope i made myself clear :)

engineerdeep commented 4 years ago

Hi @rodrigobastosv,

Oh yes, I get it. For some reason I was assuming I wouldn't have to pass the repository if in my bloc package I pass the repository through the constructor . I would have to create the bloc in my UI and would have to pass the repository instance anyway, my bad! Thank you so much! :)