Over a million developers have joined DZone.

CouchDB is a Great Fit for Domain Driven Design

· Cloud Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database on AWS. Try it now! Brought to you in partnership with MongoDB.

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 “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/

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}