使物体水合意味着什么?

发布于 2024-11-28 17:20:03 字数 146 浏览 0 评论 0原文

当有人谈论给物体补水时,这是什么意思?

我在网上看到一个名为 Hydrate 的 Java 项目,它可以在不同的表示形式(RDMS 到 OOPS 到 XML)之间转换数据。这是物体水合的一般含义吗?在表示之间转换数据?这是否意味着从存储的表示中重建对象层次结构?

When someone talks about hydrating an object, what does that mean?

I see a Java project called Hydrate on the web that transforms data between different representations (RDMS to OOPS to XML). Is this the general meaning of object hydration; to transform data between representations? Could it mean reconstructing an object hierarchy from a stored representation?

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

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

发布评论

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

评论(5

挽梦忆笙歌 2024-12-05 17:20:04

虽然正如 Merlyn 提到的那样,它有点多余,但根据我的经验,它指的是填充/填充对象,而不是实例化/创建它,因此当您需要精确时,它是一个有用的词。

While it is somewhat redundant vernacular as Merlyn mentioned, in my experience it refers only to filling/populating an object, not instantiating/creating it, so it is a useful word when you need to be precise.

土豪我们做朋友吧 2024-12-05 17:20:04

这是一个相当老的问题,但似乎以下术语的含义仍然存在混淆。希望这能消除歧义。

水合

当您看到诸如“正在等待数据的对象正在等待水合”之类的描述时,这是令人困惑和误导的。对象不会等待事物,水合只是用数据填充对象的行为。

以 JavaScript 为例:

const obj = {}; // empty object
const data = { foo: true, bar: true, baz: true };

// Hydrate "obj" with "data" 
Object.assign(obj, data); 
console.log(obj.foo); // true
console.log(obj.bar); // true
console.log(obj.baz); // true

任何向 obj 添加值的东西都是在“补充”它。在这个例子中我只是使用了Object.assign()

由于其他答案中也提到了术语“序列化”和“反序列化”,因此以下示例可帮助消除这些概念与水合作用的含义:

序列化

console.log(JSON.stringify({ foo: true, bar: true, baz: true }));

反序列化

console.log(JSON.parse('{"foo":true,"bar":true,"baz":true}'));

This is a pretty old question, but it seems that there is still confusion over the meaning of the following terms. Hopefully, this will disambiguate.

Hydrate

When you see descriptions that say things like, "an object that is waiting for data, is waiting to be hydrated", that's confusing and misleading. Objects don't wait for things, and hydration is just the act of filling an object with data.

Using JavaScript as the example:

const obj = {}; // empty object
const data = { foo: true, bar: true, baz: true };

// Hydrate "obj" with "data" 
Object.assign(obj, data); 
console.log(obj.foo); // true
console.log(obj.bar); // true
console.log(obj.baz); // true

Anything that adds values to obj is "hydrating" it. I'm just using Object.assign() in this example.

Since the terms "serialize" and "deserialize" were also mentioned in other answers, here are examples to help disambiguate the meaning of those concepts from hydration:

Serialize

console.log(JSON.stringify({ foo: true, bar: true, baz: true }));

Deserialize

console.log(JSON.parse('{"foo":true,"bar":true,"baz":true}'));
美男兮 2024-12-05 17:20:04

在 PHP 中,您可以根据名称创建一个新类,无需调用构造函数,如下所示:

require "A.php";
$className = "A";
$class = new \ReflectionClass($className);
$instance = $class->newInstanceWithoutConstructor();

然后,您可以调用 setter(或公共属性)

In PHP, you can create a new class from its name, w/o invoke constructor, like this:

require "A.php";
$className = "A";
$class = new \ReflectionClass($className);
$instance = $class->newInstanceWithoutConstructor();

Then, you can hydrate invoking setters (or public attributes)

雨落星ぅ辰 2024-12-05 17:20:03

水合是指用数据填充对象的过程。尚未水化的对象已被实例化,并表示具有数据的实体,但数据尚未加载到对象中。这样做是出于性能原因。

此外,在讨论从数据库或其他数据源加载数据的计划时,会使用术语“水合”。以下是一些示例:

当您仅将部分字段加载到某个对象中而不是全部字段时,您可以说该对象已部分水合。可以这样做,因为这些其他字段对于您当前的操作来说不是必需的。因此,当不使用这些数据时,没有理由浪费带宽和 CPU 周期来加载、传输和设置这些数据。

此外,还有一些 ORM,例如 Doctrine,它们在实例化对象时不会水合对象,而是仅在访问该对象中的数据时才水合对象。这是一种有助于不加载不会使用的数据的方法。

Hydration refers to the process of filling an object with data. An object which has not yet been hydrated has been instantiated and represents an entity that does have data, but the data has not yet been loaded into the object. This is something that is done for performance reasons.

Additionally, the term hydration is used when discussing plans for loading data from databases or other data sources. Here are some examples:

You could say that an object is partially hydrated when you have only loaded some of the fields into it, but not all of them. This can be done because those other fields are not necessary for your current operations. So there's no reason to waste bandwidth and CPU cycles loading, transferring, and setting this data when it's not going to be used.

Additionally, there are some ORM's, such as Doctrine, which do not hydrate objects when they are instantiated, but only when the data is accessed in that object. This is one method that helps to not load data which is not going to be used.

娇女薄笑 2024-12-05 17:20:03

对于更通用的术语“水化”对象,

水化对象是获取内存中存在的、尚不包含任何域数据(“真实”数据)的对象,然后用域数据填充它(例如来自数据库、网络或文件系统)。

来自埃里克·罗伯逊对此答案的评论:

反序列化==实例化+水合

如果您不需要担心极高的性能,并且您没有调试数据访问 API 内部的性能优化,那么您可能不需要显式处理水化。您通常会使用 反序列化 来代替,这样您就可以编写更少的代码。某些数据访问 API 不提供此选项,在这些情况下,您还必须自己显式调用水合步骤。

有关水合概念的更多详细信息,请参阅 Erick Robertson 对同一问题的回答

关于 名为 Hydro 的 Java 项目

您具体询问了这个框架,所以我研究了它。

据我所知,我认为这个项目并没有在非常通用的意义上使用“水合物”这个词。我认为它在标题中的使用是“序列化”的近似同义词。如上所述,这种用法并不完全准确:

请参阅:http://en.wikipedia.org/wiki /序列化

将数据结构或对象状态转换为可以存储并稍后在相同或另一个计算机环境中重建的格式。

我无法直接在Hydrate FAQ上找到他们名字背后的原因,但我得到了一些线索他们的意图。我认为他们选择“Hydrate”这个名字是因为该库的用途类似于流行的类似声音的 Hibernate 框架,但它的设计考虑了完全相反的工作流程。

大多数 ORM(包括 Hibernate)都采用内存中面向对象模型的方法,其次考虑数据库。相反,Hydrate 库采用面向数据库模式的方法,保留您的关系数据结构并让您的程序在它们之上更干净地工作。

打个比方来说,仍然是关于这个库的名称:Hydrate就像“准备好使用的东西”(比如重新水合干食品)。它是 Hibernate 的隐喻对立面,后者更像是“为冬天准备一些东西”(例如 动物冬眠)。

据我所知,将库命名为 Hydrate 的决定与通用计算机编程术语“Hydrate”无关。

当使用通用计算机编程术语“水合物”时,性能优化通常是动机(或调试现有优化)。即使该库支持对对象何时以及如何填充数据进行精细控制,时间和性能似乎并不是该名称或库功能的主要动机。该库似乎更关心启用端到端映射和模式保存。

With respect to the more generic term hydrate

Hydrating an object is taking an object that exists in memory, that doesn't yet contain any domain data ("real" data), and then populating it with domain data (such as from a database, from the network, or from a file system).

From Erick Robertson's comments on this answer:

deserialization == instantiation + hydration

If you don't need to worry about blistering performance, and you aren't debugging performance optimizations that are in the internals of a data access API, then you probably don't need to deal with hydration explicitly. You would typically use deserialization instead so you can write less code. Some data access APIs don't give you this option, and in those cases you'd also have to explicitly call the hydration step yourself.

For a bit more detail on the concept of Hydration, see Erick Robertson's answer on this same question.

With respect to the Java project called hydrate

You asked about this framework specifically, so I looked into it.

As best as I can tell, I don't think this project used the word "hydrate" in a very generic sense. I see its use in the title as an approximate synonym for "serialization". As explained above, this usage isn't entirely accurate:

See: http://en.wikipedia.org/wiki/Serialization

translating data structures or object state into a format that can be stored [...] and reconstructed later in the same or another computer environment.

I can't find the reason behind their name directly on the Hydrate FAQ, but I got clues to their intention. I think they picked the name "Hydrate" because the purpose of the library is similar to the popular sound-alike Hibernate framework, but it was designed with the exact opposite workflow in mind.

Most ORMs, Hibernate included, take an in-memory object-model oriented approach, with the database taking second consideration. The Hydrate library instead takes a database-schema oriented approach, preserving your relational data structures and letting your program work on top of them more cleanly.

Metaphorically speaking, still with respect to this library's name: Hydrate is like "making something ready to use" (like re-hydrating Dried Foods). It is a metaphorical opposite of Hibernate, which is more like "putting something away for the winter" (like Animal Hibernation).

The decision to name the library Hydrate, as far as I can tell, was not concerned with the generic computer programming term "hydrate".

When using the generic computer programming term "hydrate", performance optimizations are usually the motivation (or debugging existing optimizations). Even if the library supports granular control over when and how objects are populated with data, the timing and performance don't seem to be the primary motivation for the name or the library's functionality. The library seems more concerned with enabling end-to-end mapping and schema-preservation.

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