Populating new aggregates
2015-09-02 07:05:19 GMT
This is about DDD, Aggregates and persistence.
In my opinion, aggregates should always be fully populated, i.e the complete graph should be loaded when reading from the database. I've seen code with lots of if's and specialized handling for both partial and fully loaded aggregates, and I think this complicates things more than necessary.
So, when reading from database, my strategy is to load complete aggregates. My question is what to do when creating new aggregates. (I know, you shouldn't talk about creating aggregates, since nothing appears out of nowhere, but just to simplify a bit...).
Let's say I'm creating a Car aggregate cons isting of already existing tires, steering wheels. What I often see, especially when using Enitiy Framework, is that the Car is being created with id references to the parts. Then the car is saved, and next time it is read from database, the actual steering wheel and tires are loaded too. That means, that the Car aggregate is not complete before it is saved. And this troubles me. For instance, I cannot ask the car if the tires are good enough before saving, or if the steering wheel actually fit or anything else that has to do with the parts of the car. So, I would like the car to be populated even before I save it.
What is the best way of populating an aggregate when creating it? Any thoughts?