Reputation: 885
I really like the idea of active record and Im going to implement the following design: All concrete models extends abstract model, which have basic CRUD operations.
Here is the sample save function on model:
public void save(){
try {
getDao().createOrUpdate(this);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and here is getDao():
private static Dao<Model, Integer> getDao(Context context){
Dao<Model, Integer> result = null;
DatabaseHelper dbHelper = new DatabaseHelper(context);
try {
result = dbHelper.getDao(Model.class);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
As you can see I have Model.class. Are there any other options or patterns to implement the following design, except passing a Class to getDao function?
Upvotes: 1
Views: 369
Reputation: 200170
Do not make getDao
method static and then:
result = dbHelper.getDao(getClass());
Edit:
In that case, you will have to somehow tell the getDao method what dao to get. You could use something like:
private static <T> Dao getDao(Context context, T object){
try {
return new DatabaseHelper(context).getDao(object.getClass());
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
Upvotes: 3