我的项目分为三个模块(多层架构): 应用程序(我有我的活动) BusinessLogic DAO(我正在使用 greenDAO 版本 3)
而且我需要知道从 DAO 层打开数据库会话的最佳方式,我没有任何活动(此时我像参数一样发送活动)。
文章类:
@Id
Long id;
@NotNull
String nombre;
public Articulo (){}
@Generated(hash = 742341383)
public Articulo(Long id, @NotNull String nombre) {
this.id = id;
this.nombre = nombre;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
应用程序类:
public static final boolean ENCRYPTED = false;
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
public DaoSession getDaoSession() {
return daoSession;
}
ArticuoBL.class (businessLogic) 这是我的问题,我想在哪里获得会话
private ArticuloDao artDAO;
public ArticuloBL(){}
public void crearArticulo(Activity act){
Articulo art = new Articulo();
art.setId(3l);
art.setNombre("New Article");
DaoSession daoSession = ((App) act.getApplication()).getDaoSession();
artDAO = daoSession.getArticuloDao();
artDAO.insertInTx(art);
}
谢谢