Saturday, 17 August 2013

ContentProvider, multi-tables and the correct OOP code (good practice)

ContentProvider, multi-tables and the correct OOP code (good practice)

I thought much time wondering how to constract ContentProvider class to be
convenient working with many tables (about 10 tables and 60
methods:delete, query, insert..) because I didn't want to have them all in
1 method (delete, query, insert) with 1 switch clause. So I use this
patern, for example:
in ContentProvider class:
public int delete(Uri uri, String where, String[] whereArgs) {
switch (sUriMatcher.match(uri)) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
...
switch (sUriMatcher.match(uri)) {
...
case EXAMPLE_ID:
id = Integer.parseInt(uri.getPathSegments().get(1));
count = tblExample.deleteRaw(db, id);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
I constract 1 class for each table, where I wrote static methods, which I
call from ContentProvider class, they are like this (each method much more
complex in reality):
public class tblExample {
public static final Uri CONTENT_URI = Uri.parse("content://" +
DBContentProvider.AUTHORITY + "/example");
public static final String KEY_ROWID = "_id";
public static final String ROW_ID_EXAMPLE = "id_example";
...
public static int deleteRaw(SQLiteDatabase mDb, int id_example) {
return mDb.delete("tblExample", tblExample.ROW_ID_EXAMPLE +
" = "+ id_example, null);
}
...
}
I see that this is convenient to use my pattern, but the question is ...
is this not a very ugly code from the point of oop or memory management?

No comments:

Post a Comment