PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

CouchDB and Domain-Driven Design

Note: This article was originally published at Planet PHP on 3 August 2011.
Planet PHP

I've found CouchDB to be a great fit for domain-driven design (DDD). Specifically, CouchDB fits very well with the building block patterns and practices found within DDD. Two of these building blocks include Entities and Value Objects. Entities are objects defined by a thread of continuity and identity. A Value Object aois an object that describes some characteristic or attribute but carries no concept of identity.a Value objects should be treated as immutable.

Aggregates are groupings of associated Entities and Value Objects. Within an Aggregate, one member is designated as the Aggregate Root. External references are limited to only the Aggregate Root. Aggregates should follow transaction, distribution, and concurrency boundaries. Guess what else is defined by transaction, distribution, and concurrency boundaries? That's right, JSON documents in CouchDB.

Let's take a look at an example Aggregate, that representing a blog entry and related metadata. Note that the following UML diagrams are for classes in PHP, but it should be easy enough to translate these examples to any object-oriented programming language. We'll start with the Entry Entity, which will serve as our Aggregate Root:

----------------------------------------- | Entry | ----------------------------------------- |+ id : string | |+ rev : string | |+ title : Text | |+ updated : Date | |+ authors : Person[*] | |+ content : Text | ----------------------------------------- |+ __construct(entry : array) : void | |+ toArray() : array | -----------------------------------------

The Text Value Object:

---------------------------------------------- | Text | ---------------------------------------------- |- type : string | |- text : string | ---------------------------------------------- |+ __construct(type : string, text : string) | |+ toArray() : array | ----------------------------------------------

The Date Value Object:

-------------------------------------- | Date | -------------------------------------- |- timestamp : integer | -------------------------------------- |+ __construct(timestamp : integer) | |+ __toString() : string | --------------------------------------

The Person Value Object:

------------------------------------------------------------- | Person | ------------------------------------------------------------- |- name : string | |- uri : string | |- email : string | ------------------------------------------------------------- |+ __construct(name : string, uri : string, email : string) | |+ toArray() : array | -------------------------------------------------------------

I recommend serializing each Aggregate, starting with the Aggregate Root, into a JSON document. Control access to Aggregate Roots through a Repository. The toArray() methods above return an associative array representation of each object. The Repository can then transform the array into JSON for storage in CouchDB. Let's take a look at the EntryRepository:

--------------------------------- | EntryRepository | --------------------------------- | | --------------------------------- |+get(id : string) : Entry | |+post(entry : Entry) : void | |+put(entry : Entry) : void | |+delete(entry : Entry) : void | ---------------------------------

Here's an example of what the Aggregate's object graph might look like serialized as a JSON document:

{ "_id": "http://bradley-holt.com/?p=1251", "title": { "type": "text", "text": "CouchDB and Domain-Driven Design" }, "updated": "2011-08-02T15:30:00+00:00", "authors": [ { "name": "Bradley Holt", "uri": "http://bradley-holt.com/", "email": "bradley.holt@foundline.com" } ], "content": { "type": "html", "text": "

Truncated by Planet PHP, read more at the original (another 560 bytes)