What does it mean to hydrate an object?

Viewed 121382

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?

5 Answers

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}'));

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)

Related