Question regarding the getx pattern
nxcco opened this issue · 3 comments
Hey. I really like this proposed getx file structure. I only had a few questions:
- What is the difference between a service and a provider in this case?
- What is the difference between the repositories inside the service folder and inside each module folder?
Thank you in advance!
Hello @nxcco !
The difference between providers and services are basically:
-
Providers are the source of your data, be it an API or a DB for example, then it provides the necessary information for your application.
-
Services are application services, more specifically our GetXServices, which are services / features that will be arranged by more than one part of the application, different from our controllers which, in this context, are available for each module.
I have two good examples of services for you. -
What I use most in this approach is an auth_service for example, note that each service may or may not have a repository, it depends on whether or not it has integration with providers, in the case of auth, it probably will, and in this service I concentrate everything related to authentication, such as registration, login, logout, I can use it and its respective functions of different controllers. Example, controller login will consume this service to log in, in the profile there is a button to log out and you will call this same service only with Get.find, another good use is to keep something in it, in case I keep a user or a token for example, right in all the controllers that you need this object it will already be available, without any getUser or something, soon it would take the weight of a call getUser from the controller of the profile module for example, besides you want to access his id in several other cases, for example, and your service will be available there.
-
Another good example that I used in the examples in this doc is the app_config_service.
Imagine that, just like in the example, you decide to change the theme of the app, and save it, and retrieve from GetStorage which is the last option marked to remember when reopening the app.
Soon you can do this from one or more places, at the same time wanting to recover this state to leave a white or black button, for example and this service will be available, just use Get.find.
End that is why our services are at this level of the application, in date, as it serves all modules.
See this example. -
As mentioned, controllers are responsible for controlling the module in which it is assigned, and services are not necessarily exclusive, you can call them on any controller, as long as the service is alive in memory, so it is a level above our modules , and the controllers are internal to each module, in other words, each module is the responsibility of a controller, and it is removed from memory at the right time, Services are not exclusive, you can mix in more than one module, and stay alive in memory until the app closes or you give auth_service.delete()
I will leave the issue open for today, in case there are any more questions.
Thank you for taking the time to clear things up!😄
Whenever you need us, we'll be here, see you later. xD