Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The role of Aggregate roots in a REST API (DDD)

I'm creating a new REST/hypermedia API for an on line auction service.

I'm using this as an exercise to better understand Domain Driven Design approach as for the most part it seems like a good approach.

An example of some of my entities are: Item, Listing, Bid, Purchase, BidHistory etc. I identify the Listing entity as an aggregate root through which I plan to manage Bid, Item etc.

As far as I can tell, the concept of the aggregate root is applicable to my persistence/domain layer and should NOT be a concern of my view layer (in my case JSON or XML resource representations).

Is this the case? And if so, would this mean that it is still okay to expose non aggregate root resources via URI endpoints in my REST API or am I 'constrained' to only exposing aggregate roots through my API's endpoint?

My thought is that aggregate roots are in the realm of the persistence object which is conceptually separate from the domain model and so I should be able to expose both URI such as:

GET /api/v1/listing/465489

and

GET /api/v1/listing/465489/item

regardless of whether or not Listing is the aggregate root of Item or not.

Am I on the right lines here or do I need to adjust my understanding of this before I begin to implement any code?

like image 976
apostrophedottilde Avatar asked Oct 28 '17 20:10

apostrophedottilde


People also ask

What is aggregate root in DDD?

An aggregate root is a class which works as an entry point to our aggregate. All business operations should go through the root. This way, the aggregate root can take care of keeping the aggregate in a consistent state. The root is what takes cares of all our business invariants.

What is root aggregation?

The root aggregate contains the root volume, which contains special directories and configuration files that help you administer the storage system. The following facts apply to the root aggregate: Starting with Data ONTAP 8.1, new systems are shipped with the root volume in a 64-bit root aggregate.

What is aggregate root in C#?

Aggregate root are cluster / group of objects that are treated as a single unit of data. I am sure lots of developers are already using this pattern unknowingly, via this short note I would like to inform you formally what you are doing.

How do you choose the aggregate root?

When choosing an aggregate root you choose between Transactional Consistency and Eventual Consistency. When your business rules allow you would rather favour Eventual Consistency.


1 Answers

I'm using this as an exercise to better understand Domain Driven Design approach as for the most part it seems like a good approach.

OK... but they are orthogonal concerns, so be careful.

As far as I can tell, the concept of the aggregate root is applicable to my persistence/domain layer and should NOT be a concern of my view layer (in my case JSON or XML resource representations).

That's right - aggregates are a partitioning of your business domain. Resources are part of your integration domain. See the Jim Webber talk REST - DDD in the Large.

would this mean that it is still okay to expose non aggregate root resources via URI endpoints in my REST API or am I 'constrained' to only exposing aggregate roots through my API's endpoint?

It depends how you mean that. You aren't "exposing" your aggregate roots in any case, you are adapting your domain model for the web. Which means your clients are manipulating resources, and as a side effect of this the resources manipulate the domain model.

So the messages that you send to the domain model from the api are still going to be addressed to the aggregate root, and further traversal will be required to get to the non root entities.

For operations that are safe, you don't really need the aggregate roots at all. The CQRS pattern is built on this idea; that you can read a previously cached representation of the state of the model without risking the business invariant. So creating resources with immutable semantics that provide access to entities in your domain is fine.

Unsafe operations, however, are trickier -- you need to take the semantics of the uniform interface that you expose, and translate that into the appropriate message(s) to send to the aggregate root - because it is behind the root that the authority for validating the changes actually lives.

like image 128
VoiceOfUnreason Avatar answered Sep 18 '22 17:09

VoiceOfUnreason