How do I create a helper method which accepts unknown class types and return the correct object

20 views
Skip to first unread message

Luke Alvin Madzedze

unread,
Mar 27, 2016, 12:03:53 PM3/27/16
to ORMLite Android Users

I have the following line of code for ORMLITE to create a dao (Data access object). I am trying to avoid repeating myself for ALL entities by writing the same statement for different database tables/entities.


Dao<Entity, String> entityDao = DaoManager.createDao(connectionSource,Entity.class);


How can i create a method which accepts ANY Entity that i have and return the correct entityDao object.

Such that I can combine the logic


Dao<UserEntity, String> userEntityDao = DaoManager.createDao(connectionSource,UserEntity.class);


and


Dao<AccountEntity, String> accountEntityDao = DaoManager.createDao(connectionSource,AccountEntity.class);


I have tried the following method:

  private Dao<?, String> createDAO(Class<?> dataClass) throws SQLException{
        // instantiate the dao
        return DaoManager.createDao(connectionSource, dataClass);
    }


Gray Watson

unread,
Mar 28, 2016, 10:28:38 AM3/28/16
to ORMLite Android
This should work:

private <T, ID> Dao<T, ID> createDAO(Class<T> dataClass) throws SQLException {
...

See the DoaManager createDao(...) code:

https://github.com/j256/ormlite-core/blob/master/src/main/java/com/j256/ormlite/dao/DaoManager.java#L41
> --
> You received this message because you are subscribed to the Google Groups "ORMLite Android Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ormlite-andro...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages