Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to select ORM?

@hotchemi
April 29, 2015

How to select ORM?

@hotchemi

April 29, 2015
Tweet

More Decks by @hotchemi

Other Decks in Programming

Transcript

  1. Agenda 1. Who am I? 2. How to select ORM?

    3. ActiveAndroid 4. DBFlow 5. Conclusion
  2. Who am I? • Name: Shintaro Katafuchi • GitHub, Twitter:

    @hotchemi • Library: Android-Rate https:/github.com/hotchemi/Android-Rate • From Japan, just sightseeing! • Member of DroidKaigi
  3. • Held in Tokyo 025/04/2015 • Over 400 participants •

    22 session • http://droidkaigi.github.io • We’re planning Droidcon Tokyo… DroidKaigi
  4. How to select ORM? 1. Easy to use 2. Kind

    documentation and support 3. Well known or not 4. Active development 5. Good performance
  5. The good parts • Pretty popular(★2383 in GitHub) • Quite

    easy to use • Enough documentation and information • Using with ContentProvider
  6. @Table(name = "Items") public class Item extends Model { @Column(name

    = "Name") public String name; @Column(name = "Category") public Category category; } Model
  7. Save and delete Category restaurants = new Category(); restaurants.name =

    "Restaurants"; restaurants.save(); restaurants.delete();
  8. The bad parts • Migration isn’t type safe • Difficulty

    with complex join • Using a lot of reflection • The performance is not so high • Development isn’t active… • Because of Ollie? It isn’t active neither :)
  9. Complex join final From query = new Select("Genres.genre_id", "Genres.name as

    genreName", "Genres.category_id", "Categories.name as categoryName") .from(Genre.class) .innerJoin(Category.class) .on("Genres.category_id = Categories.category_id") .where("Genres.active = 1 and Categories.active = 1") .orderBy("Categories.sort, Genres.sort"); final Cursor cursor = Cache.openDatabase().rawQuery(query.toSql(), query.getArguments());
  10. Migration ALTER TABLE Items ADD COLUMN color INTEGER; ALTER TABLE

    Items ADD COLUMN name TEXT; ALTER TABLE Items ADD COLUMN kind TEXT; BTTFUTNJHSBUJPOT/FX7FSTJPOTRM
  11. The good parts • Enough popular(★761 in GitHub) • Fullset

    API(ModelView, observable, multiple database) • Enough documentation • Active development • The performance is higher than ActiveAndroid • Using APT
  12. Initialize @Database(name = AppDatabase.NAME, version = AppDatabase.VERSION) public class AppDatabase

    { public static final String NAME = "App"; public static final int VERSION = 1; } @Table(databaseName = AppDatabase.NAME) public class TestModel1 extends BaseModel { @Column(columnType = Column.PRIMARY_KEY) public String name; }
  13. ModelView @ModelView(query = "SELECT time from AModel where time >

    0", databaseName = AppDatabase.NAME) public class AModelView extends BaseModelView<AModel> { @Column long time; }
  14. Migration @Migration(version = 2, databaseName = TestDatabase.NAME) public class Migration1

    extends BaseMigration { @Override public void onPreMigrate() { // called before migration, instantiate any migration query here } @Override public void migrate(SQLiteDatabase database) { // call your migration query } @Override public void onPostMigrate() { // release migration resources here } }
  15. Observable FlowContentObserver.ModelChangeListener modelChangeListener = new FlowContentObserver.ModelChangeListener() { @Override public void

    onModelChanged() { // called in SDK<14 } @Override public void onModelSaved() { } @Override public void onModelDeleted() { } @Override public void onModelInserted() { } @Override public void onModelUpdated() { } };
  16. Observable observer.addModelChangeListener(modelChangeListener); TestNotifiableModel testModel = new TestNotifiableModel(); testModel.setName("myName"); // will

    notify our observer automatically, no configuration needed! testModel.insert(async); testModel.update(async); testModel.save(async); testModel.delete(async); // when done with listener observer.removeModelChangeListener(modelChangeListener);
  17. The bad parts • Too early? • Too many functions

    • Content Provider Generation • Triggers, Indexes • Model Containers • Other than that…a lot!
  18. Comparison ActiveAndroid DBFlow Easy ̋ △ Popular ◎ ̋ Active

    development △ ̋ Documentation ̋ ̋ Perfomance ̋ ◎ Migration ☓ ̋ Function △ ◎ Simple ◎ ̋
  19. Conclusion • If you want to use a simple API,

    • →ActiveAndroid • If you want to see other’s sample or blog, • →ActiveAndroid • If you have to take care of performance, • →DBFlow • If you have to use multiple database, • →DBFlow
  20. Other than that? • ORMLite…Looks good to me but not

    simple. • GreenDAO…Performance is awesome but it’s annoying to write dao generation code. • SugarORM…It seems have good performance than AA but…not popular. • Realm…The highest performance, but not SqLite, bad migration, we can only write setter/getter to Model. • Other than that I’m not sure…