0

Anyone can suggest how to marry ORMLite with Dagger in Android app?

Assume I have following layers:
UI [Activity] --> Business [POJO] --> DAO [ORMLite]

Each layer in injected to 'superior layer' with Dagger.
ORMLite creates DAO.

Know that there are a few options to init DAO from ORMLite. Let's call these [using ORMLite naming from examples]: "1. normal, 2. no helper, 3. no base".

The most natural here seems approach "no helper".
But maybe someone tested all approaches and can list pros / cons of each?

Grzegorz Dev
  • 3,073
  • 1
  • 18
  • 22
  • To be fair, I'd need to see more code to understand the architecture here, and what it is that needs to be injected, and when it is created. – EpicPandaForce Jun 01 '15 at 20:58

1 Answers1

1

Finally I adatped approach from ORMLite's example "NoBase".

Create module which gives singleton of DatabaseHelper:

@Module(library = true, complete = false)
public class DbModule {

    @Provides
    @Singleton
    DatabaseHelper provideDatabaseHelper(Context context) {
        return new DatabaseHelper(context);
    }

    // ...

}

Where:

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context, FileSystemService fileSystemService) {
        super(context, "/Path/To/MyDbFile.db", null, DB_VERSION);
    }

    // ...

}

And use DatabaseHelper in you business modules to do DB inserts, queries, etc.

@Module(library = true, complete = false)
public class BusinessModule {

    @Provides
    @Singleton
    MyService provideMyService(DatabaseHelper databaseHelper) {
        MyService s = new MyServiceImpl(databaseHelper);
        return s;
    }

    ...

}

Note: this solution is not yet supporting transactions - this feat. still waits for implementing.

Grzegorz Dev
  • 3,073
  • 1
  • 18
  • 22