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

How to select ORM?

Ce37cf75fa85b89a33916545978c64de?s=47 @hotchemi
April 29, 2015

How to select ORM?

Ce37cf75fa85b89a33916545978c64de?s=128

@hotchemi

April 29, 2015
Tweet

More Decks by @hotchemi

Other Decks in Programming

Transcript

  1. How to select ORM? @hotchemi

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

    3. ActiveAndroid 4. DBFlow 5. Conclusion
  3. 1 Who am I?

  4. 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
  5. DroidKaigi?

  6. • Held in Tokyo 025/04/2015 • Over 400 participants •

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

  8. 03.-JUF HSFFO%"0 0MMJF "DUJWF"OESPJE 4VHBS03. 3FBMN DPVDICBTFMJUF TQSJOLMFT TDIFNBUJD

  9. None
  10. How to select ORM? 1. Easy to use 2. Kind

    documentation and support 3. Well known or not 4. Active development 5. Good performance
  11. 3 ActiveAndroid

  12. The good parts • Pretty popular(★2383 in GitHub) • Quite

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

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

    "Restaurants"; restaurants.save(); restaurants.delete();
  15. Select List<Item> items = new Select().from(Item.class).where("age > ?", 0).execute();

  16. 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 :)
  17. 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());
  18. Migration ALTER TABLE Items ADD COLUMN color INTEGER; ALTER TABLE

    Items ADD COLUMN name TEXT; ALTER TABLE Items ADD COLUMN kind TEXT; BTTFUTNJHSBUJPOT/FX7FSTJPOTRM
  19. 4 DBFlow

  20. 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
  21. Performance IUUQXXXSBJ[MBCTDPNEFWHPECqPXGBTUFTUBOESPJEPSNEBUBCBTFMJCSBSZ

  22. 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; }
  23. Category restaurants = new Category(); restaurants.name = "Restaurants"; restaurants.save(false); //

    async or not restaurants.delete(false); Save and delete
  24. List<DeviceObject> devices = new Select().from(DeviceObject.class) .where( Condition.column(DeviceObject$Table.NAME).is("Samsung-Galaxy-S5"), Condition.column(DeviceObject$Table.CARRIER).is("T- Mobile")).queryList(); Select

  25. ModelView @ModelView(query = "SELECT time from AModel where time >

    0", databaseName = AppDatabase.NAME) public class AModelView extends BaseModelView<AModel> { @Column long time; }
  26. 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 } }
  27. 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() { } };
  28. 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);
  29. The bad parts • Too early? • Too many functions

    • Content Provider Generation • Triggers, Indexes • Model Containers • Other than that…a lot!
  30. 5 Conclusion

  31. Comparison ActiveAndroid DBFlow Easy ̋ △ Popular ◎ ̋ Active

    development △ ̋ Documentation ̋ ̋ Perfomance ̋ ◎ Migration ☓ ̋ Function △ ◎ Simple ◎ ̋
  32. 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
  33. 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…
  34. It depends! Thank you!