Verified
Status Update
Comments
m6...@mschmitt.info <m6...@mschmitt.info> #2
Or alternatively, having a way to get access to RoomDatabase from within a DAO would allow me to roll my own transaction handling.
yb...@google.com <yb...@google.com> #3
yea i realized the #2 is missing. Dao needs to access to the database. @Transaction might be a convenience API but it won't solve all cases.
I was thinking that we can allow you to write a method that returns `RoomDatabase` (or instance of it). Any recommendation on that part? (if we do that, we'll probably force the Dao to be abstract class and make that method not public to enforce restrictions in the Dao interface so that it stays mockable).
abstract class MyDao {
/// MyDatabase extends RoomDatabase. Returning RoomDatabase would be valid as well.
protected abstract MyDatabase getDatabase();
}
I was thinking that we can allow you to write a method that returns `RoomDatabase` (or instance of it). Any recommendation on that part? (if we do that, we'll probably force the Dao to be abstract class and make that method not public to enforce restrictions in the Dao interface so that it stays mockable).
abstract class MyDao {
/// MyDatabase extends RoomDatabase. Returning RoomDatabase would be valid as well.
protected abstract MyDatabase getDatabase();
}
m6...@mschmitt.info <m6...@mschmitt.info> #4
Agree @Transaction wouldn't solve all cases, like transactions that involve multiple DAOs or non-DAO logic.
Re #2 My initial idea was allowing an optional one-arg constructor on the dao. But your idea sounds just as well.
abstract class MyDao {
protected MyDao(RoomDatabase database) {
// can assign database to a field and use later
}
}
public class MyDao_Impl extends MyDao {
public MyDao_Impl(RoomDatabase __db) {
super(__db);
}
}
Re #2 My initial idea was allowing an optional one-arg constructor on the dao. But your idea sounds just as well.
abstract class MyDao {
protected MyDao(RoomDatabase database) {
// can assign database to a field and use later
}
}
public class MyDao_Impl extends MyDao {
public MyDao_Impl(RoomDatabase __db) {
super(__db);
}
}
yb...@google.com <yb...@google.com> #5
actually i like that constructor argument better. It can also be typed.
I think we have 3 tasks here:
add support for @Transaction annotation on abstract class Dao methods.
add a convenience method to RoomDatabase that receives a callable and runs it in a transaction.
allow abstract Dao's to have a single argument constructor which receives an instance of a T where T extends the RoomDatabase.
I think we have 3 tasks here:
add support for @Transaction annotation on abstract class Dao methods.
add a convenience method to RoomDatabase that receives a callable and runs it in a transaction.
allow abstract Dao's to have a single argument constructor which receives an instance of a T where T extends the RoomDatabase.
Description
Version used: 1.0.0-alpha1
Devices/Android versions reproduced on: n/a
Just discovered that you also support abstract classes for DAOs, which is great! Any chance you implement transaction handling the way JDBI does? (JDBI doesn't do code generation, so not directly comparable)
The generated code could override an @Transaction annotated DAO method and wrap it in a transaction.