CouchDB is a Great Fit for Domain Driven Design
Join the DZone community and get the full member experience.
Join For FreeI’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 “is an object that describes some characteristic or attribute but carries no concept of identity.” 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": "<p>I've found CouchDB to be a great fit for…</p>" } }
You can also provide access to CouchDB views through Repositories. In
the above example, this could be through the addition of an index(skip : integer, limit : integer) : Entry[*] method to the the EntryRepository
(note that this is a naive pagination implementation, especially on
large data sets—but that’s beyond the scope of this blog post). For more
complex views, you may want to create a separate Repository for each
CouchDB view.
Source: http://bradley-holt.com/2011/08/couchdb-and-domain-driven-design/
Opinions expressed by DZone contributors are their own.
Trending
-
MLOps: Definition, Importance, and Implementation
-
What Is mTLS? How To Implement It With Istio
-
How To Design Reliable IIoT Architecture
-
Working on an Unfamiliar Codebase
Comments