Android - 将 Dao 模式与 contentProvider 结合使用
将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.