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

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;

}

Activity

  1. PerfectCarl commented on Oct 12, 2012

    @PerfectCarl
    Contributor

    I like what you are proposing, especially the @OrmLiteHelper.
    I think services should also be supported as the Service class has onCreate() and onDestroy() methods

    What about having the helper (and the dao) injected in the application object ?
    The issue being that the Application has no reliable onTerminate() method...

  2. PerfectCarl commented on Oct 14, 2012

    @PerfectCarl
    Contributor

    I'm wondering if @OrmLite(DatabaseHelper.class) should be required.
    In most cases, an android project has only one class that inherits from com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.

    So in most cases, the database helper class can be retrieved automatically. Only the minority of projects that uses many database helpers would have to specify which to use.

    What do you think ?

  3. pyricau commented on Oct 18, 2012

    @pyricau
    ContributorAuthor

    Well, even if there's only one class that inherits from com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper, how do you find such class ? Annotation processing doesn't allow to search for subclasses.

  4. PerfectCarl commented on Oct 18, 2012

    @PerfectCarl
    Contributor

    I didn't know. Oh well... :)

  5. WonderCsabo commented on Sep 2, 2014

    @WonderCsabo
    Member

    This is finally implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions