2015-06-01 127 views
0

任何人都可以建議如何在Android應用程序中與匕首結婚ORMLite?ORMLite with Dagger in Android

假設我有以下層:
UI [Activity] --> Business [POJO] --> DAO [ORMLite]

在注射用匕首「優越層」的每一層。
ORMLite創建DAO。

知道有幾個選項可以從ORMLite初始化DAO。我們稱之爲[使用ORMLite命名示例]:「1.正常,2.沒有幫手,3.沒有基礎」。

這裏最自然的似乎是「沒有幫手」的方法。
但也許有人測試了所有的方法,並可以列出每個的利弊?

+0

Dagger1 or Dagger2? – EpicPandaForce

+0

公平起見,我需要看到更多的代碼來理解這裏的體系結構,以及它需要注入什麼以及什麼時候被創建。 – EpicPandaForce

回答

1

最後我從ORMLite的例子「NoBase」中獲得了adatped方法。

創建模塊,這給DatabaseHelper的單例:

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

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

    // ... 

} 

其中:

public class DatabaseHelper extends SQLiteOpenHelper { 

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

    // ... 

} 

而且在你的業務模塊使用DatabaseHelper做DB插入,查詢等

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

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

    ... 

} 

注意:此解決方案尚未支持交易 - 這項壯舉。還在等待實施。