Android - 将 Dao 模式与 contentProvider 结合使用

发布于 2024-10-18 02:22:57 字数 642 浏览 0 评论 0原文

将 ContentProvider 与 dao 模式一起使用是正确的。 ?还是会带来性能问题?

我会尽力解释。我有一个内容提供者。一个 Activity,一个 dao 和一个 bean..

这是代码:

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();

}
....}

你觉得怎么样?

问候

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2024-10-25 02:22:57

DAO 旨在为数据库提供抽象接口。 ContentProvider 已经做到了这一点。

是的,您可以创建第二个抽象层来提供 DAO API,但是...您正在移动设备上进行编程。直接使用 ContentProvider API 会更加高效。这方面的例子有很多。例如,看看 Cursors 和 ListView 的耦合有多紧密——看看 CursorAdapter 类,您将看到它是如何设计的,可以直接从数据库光标映射到屏幕上的列表。查看 ContentObserver,了解其设计如何推送通知游标进行更新以匹配更改的数据库,然后更新 ListView 中的单个列表元素以反映该数据库的实时更改

...将花费巨大的精力重新发明轮子,试图让所有现有代码都能够通过 DAO 模型。我不了解您的应用程序,但我不确定我是否看到您从中获得的优势。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文