views:

387

answers:

3

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?

A: 

From my opinion it depends - I have the same scenario and they way I deal with this is via the ORM I'm using - nHibernate.

I have mappings for all my entities of which several are aggregate roots and one of these has several other aggregate roots as member variables. The repository for this composite aggregate root does not require references to other aggregate root repositories because nHibernate knows how to get all the data required (via the mappings).

HTH

AWC

AWC
+1  A: 

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.

Paco
A: 

@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.

Mosh