1

我正在使用以下库:

ormlite-android-4.28.jar ormlite-core-4.28.jar roboguice-2.0.jar guice-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;
    }

通过在我的 Application 类中创建一个新的 AbstractModule 。但是,我见过任何可以让我在 roboguice 2 中创建相同内容的地方。

有任何想法吗?

提前致谢。

4

1 回答 1

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)
    {

    }

}

现在一切正常!它结合了不同的解决方案......

于 2013-01-07T10:42:35.743 回答