-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Improve OrmLite support by requiring less information #289
Description
We currently require two attributes on the @OrmLiteDao annotation :
@EActivity
public class MyActivity extends Activity {
// UserDao is a Dao<User, Long>
@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)
UserDao userDao;
@OrmLiteDao(helper = DatabaseHelper.class, model = Car.class)
Dao<Car, Long> carDao;
}I think we can get rid of the model parameter, and get that information from the first generic parameter of the Dao class.
Regarding the helper parameter, the current implementation is wrong : we specify it at the annotation level, but we only have one connectionSource_ attribute (the helper is used to get the connection source), so only the helper value of the first @OrmLiteDao annotation will be used.
We should look at the code of the com.j256.ormlite.android.apptools.OrmLiteBaseActivity and copy the features.
By reading the code base, here are a few interesting things I noticed :
- The helper must extend com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
- There can only be one helper instance in the whole application.
- A helper instance is retrieved with
com.j256.ormlite.android.apptools.OpenHelperManager.getHelper(Context, Class<T>), but there is an instance count occurring, and we should callcom.j256.ormlite.android.apptools.OpenHelperManager.releaseHelper()in theonDestroy()method of the application.
So the helper could be defined at the class level, with a dedicated annotation such as @OrmLite(DatabaseHelper.class). We'd call getHelper() in onCreate(), store it as an attribute, and call releaseHelper() in onDestroy().
The main problem I see here is that this can only work for activities (not any @EBean), at least with our current codebase.
Also, there there could be a @OrmLiteSource to inject a ConnectionSource, and a @OrmLiteHelper to inject the database helper.
So we would end up with :
@EActivity
@OrmLite(DatabaseHelper.class)
public class MyActivity extends Activity {
@OrmLiteDao
UserDao userDao;
@OrmLiteDao
Dao<Car, Long> carDao;
@OrmLiteHelper
DatabaseHelper helper;
@OrmLiteSource
ConnectionSource source;
}