我只是想用ORMLite设置我的Android项目。我正在使用Roboguice进行DI。 现在我的问题是,这里是否有人可以帮助那些合作。
我已设置扩展OrmLiteSqliteOpenHelper
的助手类。现在我想知道如何注入正确的DAO类。
一般的最佳做法会很棒。由于使用OrmLiteBaseActivity
不应该真正适用,因为这应该由Roboguice处理。问题只是:如何?
我非常感谢任何帮助,经验,最佳实践等。
答案 0 :(得分:12)
如果您从OrmLiteBaseActivity扩展,则无法从RoboActivity扩展。那没关系,只需调用以下内容(假设roboguice 1.1)对您的非生物活动进行注射:
((InjectorProvider)getApplicationContext()).getInjector().injectMembers(this)
完成后,您可以执行dao对象的注入。
要注入您的DAO,我建议您遵循SystemServiceProvider(class和bindings)建立的模式。因此,实现如下的DaoProvider:
class DaoProvider<T> implements Provider<T> {
protected ConnectionSource conn;
protected Class<T> clazz;
public DaoProvider( ConnectionSource conn, Class<T> clazz ) {
this.conn = conn;
this.clazz = clazz;
}
@Override
public T get() {
return DaoManager.createDao( conn, clazz );
}
}
提供绑定。您需要为要注入的每种DAO类型执行一次:
bind(MyDaoObjectType.class).toProvider(
new DaoProvider<MyDaoObjectType>(conn,MyDaoObjectType.class));
然后您可以将其注入您的活动或其他任何地方:
@Inject MyDaoObjectType myDaoObjectType;
答案 1 :(得分:3)
我遇到了同样的问题并最终创建了一个OrmLiteActivityListener,以收听我的RoboActivity并处理它引发的事件。
public class RoboOrmActivityBase<H extends IOrmLiteSqliteOpenHelper> extends RoboActivity {
@Inject RoboOrmLiteActivityListener<H> OrmActivityListener;
/*
* Get a helper for this action.
*/
public H getHelper() {
return OrmActivityListener.getHelper();
}
/**
* Get a connection source for this action.
*/
public ConnectionSource getConnectionSource() {
return OrmActivityListener.getHelper().getConnectionSource();
}
}
RoboOrmLiteActivityListener类派生自OrmLiteActivityBase类。 我删除了它的继承并让它听了。
public class RoboOrmLiteActivityListener<H extends IOrmLiteSqliteOpenHelper> {
private volatile H helper;
private volatile boolean created = false;
private volatile boolean destroyed = false;
OrmLiteSqliteOpenHelperProvider databaseHelperProvider;
@Inject
public RoboOrmLiteActivityListener(OrmLiteSqliteOpenHelperProvider databaseHelperProvider) {
this.databaseHelperProvider = databaseHelperProvider;
}
/**
* Get a helper for this action.
*/
public H getHelper() {
if (helper == null) {
if (!created) {
throw new IllegalStateException("A call has not been made to onCreate() yet so the helper is null");
} else if (destroyed) {
throw new IllegalStateException(
"A call to onDestroy has already been made and the helper cannot be used after that point");
} else {
throw new IllegalStateException("Helper is null for some unknown reason");
}
} else {
return helper;
}
}
/**
* Get a connection source for this action.
*/
public ConnectionSource getConnectionSource() {
return getHelper().getConnectionSource();
}
// As you might expect, some events can have parameters. The OnCreate event
// has the savedInstanceState parameter that Android passes to onCreate(Bundle)
public void onCreate(@Observes OnCreateEvent onCreate) {
//Ln.d("onCreate savedInstanceState is %s", onCreate.getSavedInstanceState())
if (helper == null) {
helper = getHelperInternal();
created = true;
}
}
public void onDestroy(@Observes OnDestroyEvent onDestroy) {
releaseHelper(helper);
destroyed = true;
}
/**
* This is called internally by the class to populate the helper object instance. This should not be called directly
* by client code unless you know what you are doing. Use {@link #getHelper()} to get a helper instance. If you are
* managing your own helper creation, override this method to supply this activity with a helper instance.
*
* <p>
* <b> NOTE: </b> If you override this method, you most likely will need to override the
* {@link #releaseHelper(OrmLiteSqliteOpenHelper)} method as well.
* </p>
*/
private H getHelperInternal() {
@SuppressWarnings("unchecked")
H newHelper = (H) databaseHelperProvider.get();
return newHelper;
}
/**
* Release the helper instance created in {@link #getHelperInternal(Context)}. You most likely will not need to call
* this directly since {@link #onDestroy()} does it for you.
*
* <p>
* <b> NOTE: </b> If you override this method, you most likely will need to override the
* {@link #getHelperInternal(Context)} method as well.
* </p>
*/
private void releaseHelper(H helper) {
OpenHelperManager.releaseHelper();
helper = null;
}
}
然后我有一个DatabaseHelperProvider,它提供了从OrmLiteSqliteOpenHelper扩展的Helper。从OpenHelperManager检索后,此提供程序手动执行对我的帮助程序的注入。
public class OrmLiteSqliteOpenHelperProvider implements Provider<OrmLiteSqliteOpenHelper> {
private final Context context;
private final Injector injector;
@Inject
public OrmLiteSqliteOpenHelperProvider(Context context, Injector injector) {
this.context=context;
this.injector=injector;
}
public OrmLiteSqliteOpenHelper get() {
//The OpenHelperManager built the DatabaseHelper, not us. So we need to guice it up manually.
//Guice normally does its injections when it does its constructions.
OrmLiteSqliteOpenHelper dbhelp = OpenHelperManager.getHelper(context);
injector.injectMembers(dbhelp);
return dbhelp;
}
}
我还有一个名为IOrmLiteSqliteOpenHelper的接口,它包含从OrmLiteSqliteOpenHelper中提取的成员。然后当我为我的助手构建我的接口(IDatabaseHelper)时,它从OrmLiteSqliteOpenHelper派生,我可以从IOrmLiteSqliteOpenHelper扩展。
然后在我的模块中绑定
static {
OpenHelperManager.setOpenHelperClass(DatabaseHelper.class);
}
和
@SuppressWarnings("unchecked")
private void bindDataHelperProvider() {
bind(IDatabaseHelper.class)
.toProvider((Class<? extends Provider<? extends IDatabaseHelper>>) OrmLiteSqliteOpenHelperProvider.class);
}
然后我的Activity将RoboGuice和Ormlite的所有内容完全在基类中处理,如下所示:
public class MyActivity extends RoboOrmActivityBase<IDatabaseHelper> {
List<IMyEntity> lis;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.routine);
try {
Dao<IMyEntity,Integer> myDao = getHelper().getMyDao();
lis = myDao.queryForAll();
答案 2 :(得分:1)
这不是最佳实践,只是我提出的解决方案,这是有效的。
请留下备注以便改进。
我在这里使用UserLocation实体作为示例 为了将SqliteOpenHelper和Dao绑定为单例,我将其添加到Module配置中:
bind(new TypeLiteral<SqliteOpenHelper>(){}).toProvider(OrmLiteSqliteOpenHelperProvider.class).in(Singleton.class);
bind(new TypeLiteral<Dao<UserLocation, Integer>>(){}).toProvider(DaoUserLocationProvider.class).in(Singleton.class);
OrmLiteSqliteOpenHelperProvider如下所示:
public class OrmLiteSqliteOpenHelperProvider implements Provider<SqliteOpenHelper>
{
@Inject Context context;
@Override
public SqliteOpenHelper get() {
return (SqliteOpenHelper) OpenHelperManager.getHelper(context);
}
}
DaoUserLocationProvider如下所示:
public class DaoUserLocationProvider implements Provider<Dao<UserLocation, Integer>>
{
@Inject SqliteOpenHelper sqliteOpenHelper;
@Override
public Dao<UserLocation, Integer> get() {
try {
return sqliteOpenHelper.getUserLocationDao();
} catch (SQLException e) {
Ln.e(e);
}
return null;
}
}
现在我可以注入Dao liek:
@Inject private Dao<UserLocation, Integer> userLocationDao;
这样我可以继续使用roboguice活动,并且roboguice将SqliteOpenHelper保存为单例。
但是关于破坏/释放的问题仍然存在。我很想复制粘贴OrmLiteBaseActivity的内容并扩展RoboActivity。 有什么意见吗?
答案 3 :(得分:0)
http://abdennour-insat.blogspot.com/2012/10/using-ormlite-for-sqlite-with-android.html
你可以关注这个tuto。
我使用特定的软件架构来避免多重继承 我在tuto开头说:
使用ormlite的类扩展
OrmliteActivity。但是当你使用GreenDroid时
,活动延伸了GDActivity。
不幸的是,Java不支持多个
继承,即JAVA类只能扩展一个
超类最大值。