2013-01-04 17 views
1

我使用下列庫:使用OrmLiteSqliteOpenHelper與DAO類

ormlite-Android的4.28.jar ormlite核心 - 4.28.jar roboguice-2.0.jar 吉斯-3.0-no_aop.jar guice-assistedinject-3.0-rc2.jar android-support-v13.jar

我所有的注入服務工作正常,但我有以下情況的問題。我已經創建了一個DaoProvider,如下所示:

public class DaoProvider<T extends DatabaseEntity, ID> implements Provider<Dao<T, ID>> 

我的AbstractModule類看起來像這樣;

bind(new TypeLiteral<Dao<CityPersist, Integer>>() { 
     }).toProvider(new DaoProvider<CityPersist, Integer>(ormLiteSqliteOpenHelper.getConnectionSource(), CityPersist.class)) 

我的CityDao看起來像這樣;

@ImplementedBy(CityDaoImpl.class) 
public interface CityDao 
    extends Dao<CityPersist, Long> 
{ 
    ConnectionSource getConnectionSource(); 

    CityPersist create(JSONObject json); 

    CityPersist findByCityId(String cityId); 
} 

問題我試圖在AbstractModule中創建ConnectionSource。如果使用roboguice 1,我可以設置以下內容;

public ClientServicesModule(OrmLiteSqliteOpenHelper ormLiteSqliteOpenHelper) 
    { 
     super(); 
     this.ormLiteSqliteOpenHelper = ormLiteSqliteOpenHelper; 
    } 

通過在我的應用程序類中創建一個新的AbstractModule。不過,我見過任何地方,允許我在roboguice 2中創建相同的圖案。

任何想法?

在此先感謝。

回答

0

找到了解決辦法;

由abstractmodules類更新如下;

bind(DealsDao.class).toProvider(new DaoProvider<DealPersist, DealsDao>(OpenHelperManager.getHelper(context, DatabaseHelper.class).getConnectionSource(),Deal.class)).in(Scopes.SINGLETON); 

然後我的助手類看起來像這樣;

public class DatabaseHelper 
    extends OrmLiteSqliteOpenHelper 
{ 

    private static final String LOG_TAG = "DatabaseHelper"; 
    private static final String DATABASE_NAME = "Data.db"; 
    private static final int DATABASE_VERSION = 1; 
    private Dao<Deal, Integer> dealDAO = null; 

    public DatabaseHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    /** 
    * This is called when the database is first created. Usually you should call createTable statements here to create the tables that will store 
    * your data. 
    */ 
    @Override 
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) 
    { 
     try 
     {   
      TableUtils.createTable(connectionSource, Deal.class); 
     } 
     catch (SQLException ex) 
     {   
      throw new RuntimeException(ex); 
     } 
    } 

    /** 
    * This is called when your application is upgraded and it has a higher version number. This allows you to adjust the various data to match the 
    * new version number. 
    */ 
    @Override 
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) 
    { 

    } 

} 

現在一切正常!它結合了不同的解決方案...