SQLiteDatabase和ContentResolver的漂亮API:storio

jopen 10年前

StorIO — modern API for SQLiteDatabase and ContentResolver

Overview:
  • Powerful & Simple set of Operations:Put,Get,Delete
  • API for Humans: Type Safety, Immutability & Thread-Safety
  • Convenient builders with compile-time guarantees for required params. Forget about 6-7nullin queries
  • Optional Type-Safe Object Mapping, if you don't want to work withCursorandContentValuesyou don't have to
  • No reflection in Operations and no annotations in the core, alsoStorIOis not ORM
  • Every Operation overStorIOcan be executed as blocking call or asrx.Observable
  • RxJavaas first class citizen, but it's not required dependency!
  • Reactive:rx.ObservablefromGetOperation can observe changes inStorIOand receive updates automatically
  • StorIOis replacements forSQLiteDatabaseandContentResolverAPIs
  • StorIO+RxJavais replacement forLoadersAPI
  • We are working onMockStorIO(similar to MockWebServer) for easy unit testing

Why StorIO?
  • Open Source -> less bugs
  • Documentation, Sample app and Design tests -> less bugs
  • StorIOhas unit and integration tests -> less bugs
  • Simple concept of just three main Operations:Put,Get,Delete-> less bugs
  • Almost everything is immutable and thread-safe -> less bugs
  • Builders for everything make code much, much more readable and obvious -> less bugs
  • Less bugs -> less bugs

Some examples

Get list of objects from SQLiteDatabase
List<Tweet> tweets = storIOSQLite    .get()    .listOfObjects(Tweet.class) // Type safety    .withQuery(Query.builder() // Query builder      .table("tweets")      .where("author = ?")      .whereArgs("artem_zin") // Varargs Object..., no more new String[] {"I", "am", "tired", "of", "this", "shit"}      .build()) // Query is immutable — you can save it and share without worries    .prepare() // Operation builder    .executeAsBlocking(); // Control flow is readable from top to bottom, just like with RxJava

Put something to SQLiteDatabase
storIOSQLite    .put() // Insert or Update    .objects(someTweets) // Type mapping!    .prepare()    .executeAsBlocking();

Delete something from SQLiteDatabase
storIOSQLite    .delete()    .byQuery(DeleteQuery.builder()      .table("tweets")      .where("timestamp <= ?")      .whereArgs(System.currentTimeMillis() - 86400) // No need to write String.valueOf()      .build())    .prepare()    .executeAsBlocking();

项目主页:http://www.open-open.com/lib/view/home/1436164834348