Flutter Firebase Database Class in multiple files - flutter

I have a big project and manage a lot of data with firebase. For this I have a class "MyFirestoreDatabase" in which I have every single firebase function, which I then call from my providers.
The problem is, that the MyFirestoreDatabase class has gotten really really big and I would want to split it up into sub classes and different files.
Every time I call a firebase function I use MyFirestoreDatabase.instance.functionName(),
so I don't think I want different classes, because then I would have multiple instances of the database open at the same time right?
Would it work to extend the class?

Calling FirebaseFirestore.instance always returns the same (default) instance, no matter how many times you call it. This is the essence of the singleton pattern.
So calling it in each separate class won't make any change in resource consumption, nor in the number of connections to the backend servers.

Related

Flutter - passing an instance of a class, or using multiple instances?

Question for Flutter users.
I have a class which holds a number of static values regarding a project. These values need to be accessible in various widgets.
Is it more efficient to create one instance of that class in the main.dart file, and pass it's pointer through to all the widgets that need it, or should each widget create it's own instance of the class? Given that the parameters within the class will never change.
I suspect both is fine (especially when there are only 20 or so parameters), but I want to select the more efficient approach.

Storing app wide instance. Static field vs pass instance as argument

Say I have an instance of a class which i need access to in many different places of my app. I have come up with three solutions so far
Passing the instance as an argument in every class of the app
Making a top level Provider above the MaterialApp-widget that exposes the instance to every method that has access to the app's context
Storing the instance in a static field
Which way is the best performance wise? Will the Flutter-framework ever discard the instance stored in the static field?
Which way is the best performance wise? Will the Flutter-framework ever discard the instance stored in the static field?
In this case, I like to use the DI framework like get_it
With the DI you can specify what is the object that you want and you can have it in your Wiget or Component, without coupled it with the specific implementation.
Use the get started guide to see how it is simple to use and what are the benefits.
Get Started
Suggestion
I suggest wrapping the library in one component inside your app, with the only motivation that with your own wrapper around the DI library you avoid to coupled all the app with the get_it, and you can change it easily in the future if you want.
Not sure about the performance, but the second option (Provider way) is what I am using in my projects and that's even a recommended approach (https://flutter.dev/docs/development/data-and-backend/state-mgmt/options#provider).
Passing the instance as an argument in every class of the app
This could lead to a very cumbersome code, every constructor would be cluttered by the same property passing all the way down to the Widget tree - not a Flutter way at all, that's why InheritedWidget was introduced some time ago, and later - Provider.
Storing the instance in a static field
It depends. E.g. it is ok to store all the colour constants inside a class having static fields - that's just convenient to access them everywhere. This option could also work in your case (not sure about the size and the amount of information stored in that class instance), but implementing your class as a Singleton would probably make more sense than storing the whole instance in a static field.

Generated classes

I try to sync data via GraphQL from my backend. Therefore I use the artemis package to generate my classes. However I then want to cache the data localy and therefore use the sqfentity_gen package to generate classes to safe my data via sql. I can use a json constructor with each framework to convert the data between.However I want to encapsulate certain functionality since I dont want to just safe changed data localy but sync it to the backend and handle certain errors like merge conflicts or missing network. Therefore I am thinking about wrapping the classes with an other one since I cant change the generated code. Is this a good idea or are there other solutions which work better? Would you use a completly diffrent setup? Glad for any suggestions
Instead of generate and/or re-generate (update) classes which are based on db-tables (I assume), you can use solution from the box, in example, NReco.GraphQL
It allows you set db-schema in the json file and just set db-connection. If you have a lot of tables, you can just generate that json file based on metatable info.
Storing and updating classes, from my point of view is useless.

Where to store database connection using Slick standalone

I found many examples online that put a call to Database.forConfig inside a trait, and each repository extends this trait. Some examples:
https://github.com/BBartosz/akkaRestApi/blob/master/src/main/scala/utils/DatabaseConfig.scala
https://github.com/Platoonhead/SlickWithScala/blob/master/src/main/scala/com/edu/knoldus/connection/ConnectedDbMysql.scala
https://github.com/cdiniz/slick-akka-http/blob/master/src/main/scala/utils/PersistenceModule.scala
Will it lead to creating too many instances of the DB client object, memory overhead, any other performance problems, when having many repositories?
Isn't it better to have one object that will call Database.forConfig and will have a link to the database?
What is the best practice here?
Here is an example of how I did it:
https://github.com/joesan/plant-simulator/blob/master/app/com/inland24/plantsim/config/AppConfig.scala
So what I basically do is that, I create a single copy of the variable that will call the Slick API and then I specify the number of threads (effectively the number of connections) that I want in the connection pool.
http://slick.lightbend.com/doc/3.0.0/database.html

Right design pattern for routinely computed values displayed in view inside Zend Framework

I'm having a design problem inside my Zend app and I don't really see where I should plug this feature to have a design integrated correctly inside Zend Framework.
What I want is to displayed inside every public page of my app some stats like the count of entries in the database (like the number of users). I need to have a dedicated function to do this computation since I want to cache its result.
I've to add that part of my actions are acting as JSON REST ressources (for AJAX purposes) while others behave like common HTML dynamic pages (thus setting an object in all views object isn't perfect).
I'm thinking about the following solutions :
Adding a controller plugin that would be setting an object containing my stats values inside the Zend_Registry, this plugin would handle the cache
Adding inside my models a function enabling the computation of those stats. This would imply that the feature would be exploded over all my models (which is not really a clean design), these models will cached some values (which is not their job TMHO) and this will make these computations trigger by the view (I usually let the controller load the value inside the view)
Does anyone has any suggestions or comments about a cleaner solution ?
My criterions are :
Integration in Zend Framework design philosophy
Respect of model integrity (a cache value means an inexact value)
Avoiding of workarounds like using static ressources like Zend_Registry
I would do it this with a view helper and a cron job.
Create relevant model classes that do to the computation
Create a service class with two methods:
A method that calculates the results by using the model classes and caches them.
A method that returns the cached results.
Create a view helper that uses the service class to get the results.
Set up a cron job that uses a cli script to call method 2.1 every 15 mins or however frequently you need it.