[Discussion] is it good choice to remove all the repositories as they are just a delegate calls? #97
-
Hi @arafaysaleem, thanks for sharing this project, it's awesome and very detailed. One question on repositories, as the code in repositories have a single call to ApiService, can all the repositories be removed and have the calls to ApiService in Providers? Would it be required to have repository layer especially if there is no direct communication with any persistent layer? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Nice observation @codinesh. There are a couple of reasons why I don't like to mix up repositories and providers. While it may look like repositories hold no special logic and can be overriden, that's no true. In this context, repositories are part of the data layer. Whereas providers are part of the domain layer. The objects in the domain layer shouldn't have to worry about fetching the data from the datasource, parsing to validated entities etc. All these tasks should be handled in the data layer like so:
Moreover, if you ever want to perform data caching that logic is going to bloat the provider and it would be doing too much, which is a violation of the Single Responsibility Principle. Caching logic goes with the repositories as well. Also, it is helpful to implement strong interfaces. For e.g. in the future if I want to switch out my data source to firebase, I can create a new class and implement the original repository to get all the necessary method definitions, provide my own firebase logic and simply swap out the repository I am passing to the provider. There's no need to touch the provider code. Whereas, if my repository logic lived inside the provider I couldn't implement it as a contract because it can have other methods as well that I might not need. Finally, during testing I can simply mock the repository to return fake response and the provider won't need any modifications. Hope this helped :) |
Beta Was this translation helpful? Give feedback.
Nice observation @codinesh. There are a couple of reasons why I don't like to mix up repositories and providers.
While it may look like repositories hold no special logic and can be overriden, that's no true. In this context, repositories are part of the data layer. Whereas providers are part of the domain layer. The objects in the domain layer shouldn't have to worry about fetching the data from the datasource, parsing to validated entities etc. All these tasks should be handled in the data layer like so:
Repositories isolate domain models (or entities) from the implementation details of the data sources in the data layer.
Repositories convert data transfer objects to validated entit…