-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kiwi DI for abstract classes #46
Comments
First of all, @letsar has created this package, I have taken over the maintenance since 2 weeks. So all the credits go to him. For your question: Floor has a floor_generator that generates the non abstract class. If you want to register the abstract class you will have to register an instance that comes from the generated database. Personally, I have never used floor but by reading the readme on pub.dev I think that is the way to go. I use moor because it generates the dao classes in a separate file and the dao needs a database. So that will be injected with kiwi as well. I will have some spare time on Thursday so I will play around with Floor and I will let you know what the best sollution is. |
@vanlooverenkoen Thanks, It's okay to use Repository class as a singleton? because repository class contains objects which provides access to API and Database. So, by making Repository class singleton, we will be using a repository and related API and DB as a singleton too. Do I understood this part correct? kindly express your views on it. |
Yes indeed. We usually do it like this Singleton repo's On top of that every Singleton ia registered as an abstract class. So it is easier to write automated tests |
@vanlooverenkoen So, Database class and Dao's in floor are implemented as abstract class only? so does that mean it's singleton by default? |
No that does not mean it is a singleton by default. (Im not sure what the correct implementation is for floor. But I would think it is just a normal object. By using kiwi you can make sure the instance is always a singleton. Because kiwi maintains that for you. |
@vanlooverenkoen Kindly let me know if there's any way to do it using abstract class, I did't have any example for it. A big thanks in advance. |
Hello @vanlooverenkoen, I have give your advice a shot and look into generated floor file, which is as below.
Now in my main function, I have create singleton Object by following.
@vanlooverenkoen can you kindly tell me if my approach is right or not? I am accessing DataBase class through builder and using that singleton Database Object I am accessing DAO object which is defined as a class in CookDataBase abstract class. like following :
|
@jaydangar I'm sorry that it took so long. I completely forgot about this ticket. I would do the following
Using the kiwi_generator will result in some extra boilerplate code, that is because of how
And your injector file will look like this:
I personally use Moor for my db integration. It works way better with kiwi. Because the implementation of moor is better for dependency injection. I hope this answers your question. Sorry for the late response. |
Hello, First of all Thank you very much for making job really easy for DI in flutter for the beginners like me, Really appreciate this work. I want to ask that How can I create DI for abstract classes?
I am using Floor package for local database storage in flutter, which requires to create abstract classes for DAO like following. can you let me know how to DI for abstract classes?
The text was updated successfully, but these errors were encountered: