使用 ORMlite 实现具有基本 CRUD 操作的通用模型 Android
Implementing a Generic Model with basic CRUD operation using ORMlite for Android
我有一些抽象的 classes 作为我所有模型的基础。为了简单起见,我将只讨论称为模型的根抽象 class - 它需要处理一些重复的 ORMLite 操作。在每个 crud 操作之前,我需要对所有模型应用一些逻辑标准。所以关键是我可以在调用对象上实现保存、更新和删除,而不必重复代码。
abstract public class Model<MODEL extends Model, CHILD extends ChildModel> {
private final List<CHILD> mChildren = new ArrayList<>();
private Class<CHILD> mChildClass;
private Class<MODEL> mModelClass;
protected RuntimeExceptionDao<MODEL, Long> mRTEDao;
public Model(Class<MODEL> modelClass, Class<CHILD> childClass) {
this.mChildClass = childClass;
this.mModelClass = modelClass;
this.mRTEDao = App.getOpenHelper().getRTEDao(this.mModelClass);
}
// some codes omitted...
public void save() {
// Do something to the object before continuing
mRTEDao.create(this);
}
public void delete() {
// Do something to the object before continuing
mRTEDao.delete(this);
}
// some codes omitted...
}
上面的代码给我一个错误,因为 DAO 期望子 class "MODEL" 但 this 指的是 Model<模特,儿童>
目前解决此问题的方法如下:
// some codes omitted...
abstract protected void handleUpdate(); // Let the subclass handle the create/update
abstract protected void handleDelete(); // Let the subclass handle the delete
public void save() {
// Do something to the object before continuing
handleUpdate();
}
public void delete() {
// Do something to the object before continuing
handleDelete();
}
// some codes omitted...
至少我减少了每个模型的代码——但它看起来仍然不够优雅。有没有办法让我获取子 class 的 "this" 对象而不是基础模型 class?我试过投射,但这警告我这是一个未经检查的投射。
public void save() {
mRTEDao.create((MODEL)this);
}
public void delete() {
mRTEDao.delete((MODEL)this);
}
例如,project 模型 class 有一种优雅的方式来保存、更新、删除自身。但不幸的是,它缺少我在 ORMlite 中需要的功能,但由于需要生成基本模型,我似乎无法让 ORMlite 在我的项目中做同样的事情。
啊哈,我自己找到了一个很好的解决方案:)只需简单地实现一个像这样的抽象方法:
abstract protected MODEL getThis();
实现如下:
@Override
protected ExampleModel getMe() {
return this;
}
然后每当我需要 this 表现得像我正在使用的子类的 this 时:
public void save() {
// Do something to the object before continuing
mRTEDao.create(getThis());
}
public void delete() {
// Do something to the object before continuing
mRTEDao.delete(getThis());
}
我有一些抽象的 classes 作为我所有模型的基础。为了简单起见,我将只讨论称为模型的根抽象 class - 它需要处理一些重复的 ORMLite 操作。在每个 crud 操作之前,我需要对所有模型应用一些逻辑标准。所以关键是我可以在调用对象上实现保存、更新和删除,而不必重复代码。
abstract public class Model<MODEL extends Model, CHILD extends ChildModel> {
private final List<CHILD> mChildren = new ArrayList<>();
private Class<CHILD> mChildClass;
private Class<MODEL> mModelClass;
protected RuntimeExceptionDao<MODEL, Long> mRTEDao;
public Model(Class<MODEL> modelClass, Class<CHILD> childClass) {
this.mChildClass = childClass;
this.mModelClass = modelClass;
this.mRTEDao = App.getOpenHelper().getRTEDao(this.mModelClass);
}
// some codes omitted...
public void save() {
// Do something to the object before continuing
mRTEDao.create(this);
}
public void delete() {
// Do something to the object before continuing
mRTEDao.delete(this);
}
// some codes omitted...
}
上面的代码给我一个错误,因为 DAO 期望子 class "MODEL" 但 this 指的是 Model<模特,儿童>
目前解决此问题的方法如下:
// some codes omitted...
abstract protected void handleUpdate(); // Let the subclass handle the create/update
abstract protected void handleDelete(); // Let the subclass handle the delete
public void save() {
// Do something to the object before continuing
handleUpdate();
}
public void delete() {
// Do something to the object before continuing
handleDelete();
}
// some codes omitted...
至少我减少了每个模型的代码——但它看起来仍然不够优雅。有没有办法让我获取子 class 的 "this" 对象而不是基础模型 class?我试过投射,但这警告我这是一个未经检查的投射。
public void save() {
mRTEDao.create((MODEL)this);
}
public void delete() {
mRTEDao.delete((MODEL)this);
}
例如,project 模型 class 有一种优雅的方式来保存、更新、删除自身。但不幸的是,它缺少我在 ORMlite 中需要的功能,但由于需要生成基本模型,我似乎无法让 ORMlite 在我的项目中做同样的事情。
啊哈,我自己找到了一个很好的解决方案:)只需简单地实现一个像这样的抽象方法:
abstract protected MODEL getThis();
实现如下:
@Override
protected ExampleModel getMe() {
return this;
}
然后每当我需要 this 表现得像我正在使用的子类的 this 时:
public void save() {
// Do something to the object before continuing
mRTEDao.create(getThis());
}
public void delete() {
// Do something to the object before continuing
mRTEDao.delete(getThis());
}