Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Using Dao Pattern with contentProvider

Is correct to use ContentProvider with dao Pattern. ? or it will bring any performance issue ?

I will try to explain. I've a contentProvider. an activity, a dao and a bean ..

this is the code :

class Bean(){

 String name;

}

class Dao{

 Activity activity;

 public Dao(Activity activity){

 this.activity = activity;

public List<Bean> getAllBean() {

    Cursor c = activity.managedQuery(Bean.CONTENT_URI, PROJECTION,
                null, null, Bean.DEFAULT_SORT_ORDER);
    return BeanMapper.GetAllFromCursor(c);
    }
}

}

Class Activity{
.....


 onCreate(....){

  Dao dao = new Dao(this);
  List<Bean> aList = dao.getAllBean();

}
....}

what do you think ?

regards

like image 332
antonio Musella Avatar asked Feb 15 '11 18:02

antonio Musella


1 Answers

DAO is designed to provide an abstract interface to a database. ContentProvider already does this.

Yes, you can make a second abstraction layer to provide a DAO API, but... You're programming on a mobile device. Using the ContentProvider API directly is going to be more efficient. There are many examples of this. For example, look at how closely Cursors and ListViews are coupled -- Look at the CursorAdapter classes and you'll see how it's designed to directly map from a database cursor to a list on the screen. Look at ContentObserver, and see how that's designed to push-notify a cursor to update to match a changed database, and in turn, update a single list element in a ListView to reflect that database as it changes in realtime...

You're going to spend immense effort reinventing the wheel trying to get all that existing code to carry through a DAO model. I don't know your application, but I'm not sure I see the advantage you gain from it.

like image 67
jcwenger Avatar answered Nov 17 '22 07:11

jcwenger