Dagger 2: object-object loaded from the database

I am developing an android app and am using Dagger 2 to inject some objects as single items in my actions / fragments. Some objects are loaded from the database.

... Is it possible to download a database object in the background and insert it as soon as it is available? Or is it not a problem if I just load it when the singleton is initialized with the dagger? Alternatively, I can just pass a link to the actions / fragments and load the object there.

What is your approach to this problem?

+3


source to share


1 answer


This is an interesting question because it touches on what I think is the first problem that many people face, starting with Dependency Injection (DI): what types of objects should I inject , what should I need new , and in your case what should I transfer manually?

When using dependency injection (and presumably also unit testing, but that's a different story), it's important to understand the classification of the types of objects / classes you are designing:

  • Services: objects that perform some kind of action, such as business logic.
    • these are the things you want to mock in unit tests.
  • Value Objects (for our discussion also include DTOs and Entities .. think POCO or POJO): These are objects that store Information. Usually immutable model objects. Value objects do not have any dependencies on Services, i.e. You never want to type anything into them.
    • You never mock such objects in unit tests! Concrete types are used to create them and ideally Test Data Builders .

Note: this is my quick interpretation of these terms. If you've read the book on Domain Driven Design , you'll find much more precise definitions, but I think that's enough for a discussion of DI.

Misko Hevery (Father of AngularJS ;-) mentions that these terms like "service" are overloaded, especially in Android where "service" has a specific meaning, so he calls the Value objects and the Newables and Injectables services respectively. I think this is good advice.

To apply these concepts to your case: you will have some kind of class that queries the database for the object in question.

Let's say the object you are talking about Student

and it may have some immutable fields, something vague:



class Student {
    public final long id;
    public final String firstName;
    public final String lastName;
    public final String email;

    public Student(...) {
        // assignment of fields here...
    }
}

      

And then you would have some kind of object that asks for records Student

from the database, say something like this StudentRepository

here:

class StudentRepository {
    public List<Student> findAll() {
        // db access here...
    }
}

      

In this example, it Student

is a Value (newable) object and StudentRepository

is a Service (injectable).

In your code, you want to use dagger for input StudentRepository

, but you never type Student

...

It's hard to give further advice without knowing the details of what you are doing, but hopefully this answers your question: you need to pass the Entity database read from the database wherever you need it, you shouldn't be injected into anywhere.

+4


source







All Articles