8000 Improve OrmLite support by requiring less information · Issue #289 · androidannotations/androidannotations · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Improve OrmLite support by requiring less information #289

@pyricau

Description

@pyricau

cc @johanpoirier

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 call com.j256.ormlite.android.apptools.OpenHelperManager.releaseHelper() in the onDestroy() 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;

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0