kauemurakami / getx_pattern

Design pattern designed to standardize your projects with GetX on Flutter.

Home Page:https://kauemurakami.github.io/getx_pattern

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Understanding the difference between Services and Providers, along with their interaction with Repositories

jmsquillaro opened this issue · comments

Hello and thank you for providing this helpful pattern! I had a clarification question about Provider and Services and the use of Repositories for each.

Providers, from what I understand, is an entity that "gets data." You use API calls and database persistence as an example, with the snippet extension utilizing GetConnect when instantiating the class. Services seem to be a bit more confusing, perhaps because some other architectures use services as a way to pull data too. For example, what would an implementation of GetStorage use? Would it be a repository because it is still "fetching data," although just from the device itself? Or would it be a service? Is there any guideline you have to determine what to classify something as a Provider or Service?

Secondly, and relatedly to the first, is their interaction with Repositories. In this Github repo, the readme describes a repository created for "example_service" and for each module, but not for the providers. Yet in the snippets readme (https://marketplace.visualstudio.com/items?itemName=get-snippets.get-snippets), the repositories only exist for the modules. Yet again in the get_pattern_example project, posts_ repositories seems only to exist for the API provider, but there are no repositories for each module.

My question is, how many repositories should I have? Should I have one for each provider and service that abstracts methods out, and then have each module have a repository that I'd assume interfaces with the provider/service repository? Would they all just be calling the same functions or how would these repositories be laid out? For example, should it look like this?

-Data
 --Services
 ---storage_service.dart
 ----storage_service_repository.dart
 --Providers
 ---api_provider.dart
 ----api_provider_repository.dart
 -details_module
 --details_page.dart
 --details_controller.dart
 --details_repository.dart
 -home_module
 --home_page.dart
 --home_controller.dart
 --home_repository.dart

Or like this:

-Data
 --Services
 ---storage_service.dart
 ---storage_service_repository.dart
 --Providers
 ---api_provider.dart
 ---api_provider_repository.dart
 -details_module
 --details_page.dart
 --details_controller.dart
 -home_module
 --home_page.dart
 --home_controller.dart

Any guidance you can provide for this would be greatly appreciated. Thank you!

Regarding the first doubt, Providers and Services are literally the meaning of the words.?
Providers, provide data for your application, whether from a database, API, in other words, data!
In the example of an API, there are requests (functions) that can be used in more than one module, right? For example, check if the guy's token is still valid at some important moments.

This is where our repositories come in, each module has a repository ONLY to point out the calls between our controller and our provider, so if there is a function used in two modules, you will only point it out, you will not repeat it or create separate files, and it does part of an API, provider.

Services serve the entire application, unlike a controller, GetStorage itself is already a service, as you can use its instance anywhere in the application GetStorage().box.write ... , This does not prevent you from creating a storage server, for example, for ORGANIZATION reasons, leaving all functions related to storage in a service that has its instance.

Some static modules do not need a repository, the issue with the repository is that you can have several providers or one, such as storage, database, api, imagine an offline online system for example, you would need two providers, one would be called db, the other would be called api.

But in our module we will have a repository, with notes for these functions, both in the api and in the database, not a provider in the module or a request, WE JUST POINT TO THE LOCATION, making it easier to map too.

It suggests that there is a problem in the api with getStatus(), a random function, pointed out in a module, instead of going directly to the api file, which can take a minute, bringing together all the functions, go to your repository and click on the function.

So for each module we must have a page and a controller, the repository, only if your module depends on data external to the current context.

You should only have one api provider, and in each module you point your functions from your /provider/api to your reposity