【问题标题】:Using OrmLiteSqliteOpenHelper with DAO classes将 OrmLiteSqliteOpenHelper 与 DAO 类一起使用
【发布时间】:2013-01-04 19:38:17
【问题描述】:

我正在使用以下库:

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 中看到了任何允许我创建相同内容的地方。

有什么想法吗?

提前致谢。

【问题讨论】:

    标签: android sqlite module dao inject


    【解决方案1】:

    找到了解决办法;

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 2022-06-23
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2013-01-07
      相关资源
      最近更新 更多