Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Getting aggregate roots for other aggregates

I've been studying DDD for the past 2 weeks, and one of the things that really stuck out to me was how aggregate roots can contain other aggregate roots. Aggregate roots are retrieved from the repository, but if a root contains another root, does the repository have a reference to the other repository and asks it to build the subroot?

like image 472
Ed. Avatar asked Feb 14 '10 19:02

Ed.


2 Answers

@Paco: You are wrong. Repositories are not only for storing objects. If you have read Eric Evan's DDD book, you'd know that Repositories are like an object-oriented in-memory representation of data. You can use a Repository object the same way as you use a Collection. You can use indexer to get or set objects, you can use the Add() method to add a new object, you can use the Remove() method to remove an object, etc.

The Repository then uses the infrastructure to read/write data from/to the database. It can use a DataMapper for simplifying the retrieval of data from your relational database and mapping to your Entities.

like image 200
Mosh Avatar answered Sep 24 '22 06:09

Mosh


The repository doesn't build but it stores. When you using ddd, you might want to get familiar with basic persistence patterns like unit of work, identity map, lazy load, object relation mapper, query object, (dynamic) proxy. (these patterns have nothing to do with ddd, but are very useful to know). The repository is just a facade to hide the implementation of the patterns mentioned before and to abstract dataaccess in a domain driven way. Most people don't manually write their persistence infrastructure manually nowadays, especially when using ddd, you might like to look at an orm.

The actual reference with the code that converts the database records into an object will be in the datamapper. There will be references between mappingclasses in the mappingclasses itself, or created by something like a mapperfactory.

public interface IDataMapper<T>
{
   T Map(IDataReader reader);
}

You don't have to implement this code yourself, just use a tool that does it for you and try to understand how parts of the code in the tool (orm) works. Pure ddd without any orm is almost impossible without a good set of tools that save you from writing a lot of code.

like image 21
Paco Avatar answered Sep 26 '22 06:09

Paco