阅读背景:

在GAE,Java,JDO等环境下DAO是什么?

来源:互联网 

I've just started writing GAE web applications in Java so I'm kind of new to all this stuff. I use JDO for storing data. I'm reading a lot of online materials (forums, tutorials...) and I see DAO everywhere but I really don't understand what it is. Yes, Data Access Object, technique... But when someone names a variable userDAO what is the variable going to contain?

我刚刚开始用Java编写GAE Web应用程序,所以我对所有这些东西都是新手。我使用JDO来存储数据。我正在阅读很多在线资料(论坛,教程......),我到处都看到DAO,但我真的不明白它是什么。是的,数据访问对象,技术......但是当有人命名变量userDA时,变量将包含哪些内容?

Let's consider following code (from GAE documentation):

让我们考虑以下代码(来自GAE文档):

PersistenceManager pm = PMF.get().getPersistenceManager();

Employee e = new Employee("Alfred", "Smith", new Date());

try {
    pm.makePersistent(e);
} finally {
    pm.close();
} 

It's really simple, makes sense to me... but what in this example would you call DAO?

这很简单,对我来说很有意义......但是在这个例子中你会称之为DAO吗?

It is probably a stupid question but it would help me a lot.

这可能是一个愚蠢的问题,但它会帮助我很多。

1 个解决方案

#1


7  

A "DAO" stands for data access object. It's a way to encapsulate model logic by wrapping a given model entity with a class that provides more intuitive accessors.

“DAO”代表数据访问对象。这是一种通过将给定模型实体与提供更直观访问器的类包装在一起来封装模型逻辑的方法。

I am not certain about the example you provide, but I'm willing to speculate. It looks like the PersistanceManager is an object which manages your application's data persistance layer. Your Employee object is likely persistently stored through this PersistanceManager instance, and the Employee object you've constructed is probably a DAO providing a interface for managing that employee's state that is simpler than managing state through the PersistanceManager directly.

我不确定你提供的例子,但我愿意推测。看起来PersistanceManager是一个管理应用程序数据持久层的对象。您的Employee对象可能通过此PersistanceManager实例持久存储,并且您构建的Employee对象可能是一个DAO,提供了一个用于管理该员工状态的接口,该接口比直接通过PersistanceManager管理状态更简单。

On App Engine, one of the big performance constraints for the datastore is deserializing protocol buffers. If you add complicated methods to your model entities, you will increase the object size, which will cause a performance hit when you have to deserialize the object. The takeaway here is that you don't want to add anything more than the base properties onto a datastore entity specification. Adding helper methods will cause a performance hit.

在App Engine上,数据存储的一个重要性能限制是对协议缓冲区进行反序列化。如果向模型实体添加复杂方法,则会增加对象大小,这会在必须反序列化对象时导致性能损失。这里要说的是,您不希望在数据存储区实体规范中添加任何基本属性以外的内容。添加辅助方法会导致性能下降。

Thus, a common pattern on App Engine is to use a DAO to wrap the model entity with a class that can provide this higher level logic without impacting serialization performance.

因此,App Engine上的一个常见模式是使用DAO将模型实体包装为可以提供此更高级别逻辑的类,而不会影响序列化性能。


分享到: