Is there any way to create reusable generic base class DAOs with Android Room?
public interface BaseDao {
@Insert
void insert(T object);
@Up
I have a solution for findAll.
Codes in that BaseDao:
...
public List findAll() {
SimpleSQLiteQuery query = new SimpleSQLiteQuery(
"select * from " + getTableName()
);
return doFindAll(query);
}
...
public String getTableName() {
// Below is based on your inheritance chain
Class clazz = (Class)
((ParameterizedType) getClass().getSuperclass().getGenericSuperclass())
.getActualTypeArguments()[0];
// tableName = StringUtil.toSnakeCase(clazz.getSimpleName());
String tableName = clazz.getSimpleName();
return tableName;
}
...
@RawQuery
protected abstract List doFindAll(SupportSQLiteQuery query);
and other Dao looks like :
@Dao
public abstract class UserDao extends AppDao {
}
That's all
The idea is
If you prefer interface to abstract class, you can try optional method of java 8.
It's not beautiful but worked, as you can see.
I created a gist at here