<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://dddstepbystep.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Blogged Aggregates and Aggregate Roots</title><link>http://dddstepbystep.com/wikis/ddd/blogged-aggregates-and-aggregate-roots.aspx</link><description>The main DDD Step By Step wiki, containing a Glossary of DDD terms, and the blog posts from the original series.</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Blogged Aggregates and Aggregate Roots</title><link>http://dddstepbystep.com/wikis/ddd/blogged-aggregates-and-aggregate-roots.aspx</link><pubDate>Mon, 23 Feb 2009 04:30:57 GMT</pubDate><guid isPermaLink="false">b5ce9276-785b-4c0c-806a-f36147849843:24</guid><dc:creator>Jak Charlton</dc:creator><comments>http://dddstepbystep.com/wikis/ddd/blogged-aggregates-and-aggregate-roots/comments.aspx</comments><description>Current revision posted to Domain Driven Design Wiki by Jak Charlton on 2/23/2009 2:30:57 AM&lt;br /&gt;
&lt;h2&gt;Blogged Aggregates and Aggregate Roots&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Aggregate, Aggregate Root&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Blogged&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;
&lt;p style="padding-left:30px;"&gt;We are family&amp;nbsp;&lt;br /&gt;I got all my sisters with me&amp;nbsp;&lt;br /&gt;Sister Sledge&lt;/p&gt;
&lt;p&gt;Some things belong together, like Apple Pie and Ice Cream, or Sonny and Cher. And so it is with [[Entity|Entities]] and [[Value Object|Value Objects (VOs)]] &amp;ndash; some of them belong together.&lt;/p&gt;
&lt;p&gt;[[Aggregate|Aggregates]] are groups of things that belong together. An [[Aggregate Root]] is the thing that holds them all together.&lt;/p&gt;
&lt;p&gt;I will warn in advance, as I proof read this post, it was pretty complicated &amp;ndash; while I have tried to simplify the concepts, I am not certain I have totally succeeded. Hopefully [[Aggregate|Aggregates]] will become more clear later in the series when I start exploring them in code.&lt;/p&gt;
&lt;h3&gt;A Simple Example&lt;/h3&gt;
&lt;p&gt;In all Object Oriented programming we are used to dealing with objects that have references to other objects, and in DDD it is no different. For example, our Customer may have a reference to the customer&amp;rsquo;s Orders.&lt;/p&gt;
&lt;p&gt;An [[Aggregate]] is slightly different, where as Customers and Orders can exist in the system independently, some [[Entity|Entities]] and [[Value Object|Value Objects]] make absolutely no sense without their parent. The obvious example following on is Orders and OrderLines.&lt;/p&gt;
&lt;p&gt;OrderLines have no reason to exist without their parent Order, nor can they belong to any other Order. In this case, Order and OrderLines would probably be an [[Aggregate]], and the Order would be the [[Aggregate Root]]&lt;/p&gt;
&lt;p&gt;The rule of Cascading Delete is sometimes cited as a good way to tell if you have a group of [[Entity|Entities]] or [[Value Object|VOs]] that should be an [[Aggregate]] &amp;ndash; if the parent, in this case the Order, was deleted all other parts of that [[Aggregate]] below Order would be deleted too. So if it doesn&amp;rsquo;t make sense that a parent being deleted would also delete all children, then you don&amp;rsquo;t have an [[Aggregate]], you just have a good old fashioned reference.&lt;/p&gt;
&lt;h3&gt;So What is the Point of an Aggregate?&lt;/h3&gt;
&lt;p&gt;Well the first and most obvious point of an [[Aggregate]] is that it operates to a large degree as one &amp;ldquo;thing&amp;rdquo;. Notably the [[Aggregate Root]] is the single [[Entity]] that controls access to the children.&lt;/p&gt;
&lt;p&gt;Where you may have a Customer with operations like .UpdateToPreferredStatus and you may have an Order with .GetSumTotal &amp;ndash; the OrderLines of an Order would not have any logic exposed outside of the Order [[Entity]] &amp;ndash; in other words to add a new OrderLine, or to change an OrderLine, you would tell the Order to make the changes &amp;ndash; Order.AddNewItem for example.&lt;/p&gt;
&lt;p&gt;In this respect, [[Aggregate|Aggregates]] provide a clean pattern to keep logic where it really belongs.&lt;/p&gt;
&lt;p&gt;Another aspect of [[Aggregate Root|Aggregate Roots]] is that they are the [[Entity|Entities]] that are dealt with by [[Repository|Repositories]].&lt;/p&gt;
&lt;p&gt;In our examples above, we would have a Customer [[Repository]], and an Order [[Repository]], but there would not be an OrderLine [[Repository]]. It is the responsibility of the [[Repository]] for the [[Aggregate Root]] to deal with persistence of all the children of that [[Aggregate]].&lt;/p&gt;
&lt;h3&gt;Restrictions on Aggregates and Aggregate Roots&lt;/h3&gt;
&lt;p&gt;The main, and possibly obvious restriction on Aggregate Roots is, they must be Entities, and cannot be Value Objects. Back to the previous post, you will remember that [[Entity|Entities]] have Identity, and [[Value Object|Value Objects]] do not &amp;ndash; you could not ask a [[Repository]] to retrieve an [[Aggregate Root]] if it had no Identity.&lt;/p&gt;
&lt;p&gt;Within an [[Aggregate]], the other players can be&amp;nbsp;[[Entity|Entities]] and [[Value Objects|VOs]]&amp;nbsp;as the [[Domain]] dictates. For example, expanding our Order example further, the [[Aggregate]] may comprise the Order ([[Aggregate Root]]), the Order may have an OrderNumber ([[Value Object]]), some OrderLines ([[Entity|Entities]]), and a Shipping Address and Billing Address ([[Value Object|Value Objects]])&lt;/p&gt;
&lt;p&gt;Entities can hold references to any [[Aggregate Root]], but never to any other [[Entity]] or [[Value Object|VO]] within the&amp;nbsp;[[Aggregate|Aggregates]]. To access any other part of the&amp;nbsp;[[Aggregate]], you must navigate from the [[Aggregate Root]].&lt;/p&gt;
&lt;p&gt;How the component parts of an&amp;nbsp;[[Aggregate|Aggregates]]&amp;nbsp;are persisted is a matter for the implementation behind [[Repository]], but if you were using an ORM like NHibernate for example, the changes are that the [[Value Object|Value Objects]] would be NHibernate Components nested in their parent entity record and the [[Entity|Entities]] would be old fashioned mappings, each with their own table.&lt;/p&gt;
&lt;h3&gt;In Conclusion&lt;/h3&gt;
&lt;p&gt;[[Aggregate|Aggregates]] provide a logical grouping of [[Entity|Entities]] and [[Value Object|Value Objects]] that belong together at all times. An [[Aggregate Root]] is the gatekeeper to the [[Aggregate]]. Each [[Aggregate]] is treated as a single unit for persistence purposes.&lt;/p&gt;
&lt;p&gt;By logically grouping [[Entity|Entities]] and [[Value Objects|VOs]] in this way, we provide a mechanism to strictly manage a grouping of objects, and a way to allow us to treat a number of different&amp;nbsp;[[Entity|Entities]] and [[Value Objects|VOs]]&amp;nbsp;as one.&lt;/p&gt;
&lt;/p&gt;</description></item><item><title>Blogged Aggregates and Aggregate Roots</title><link>http://dddstepbystep.com/wikis/ddd/blogged-aggregates-and-aggregate-roots/revision/1.aspx</link><pubDate>Mon, 23 Feb 2009 04:30:57 GMT</pubDate><guid isPermaLink="false">b5ce9276-785b-4c0c-806a-f36147849843:91</guid><dc:creator>Jak Charlton</dc:creator><comments>http://dddstepbystep.com/wikis/ddd/blogged-aggregates-and-aggregate-roots/comments.aspx</comments><description>Revision 1 posted to Domain Driven Design Wiki by Jak Charlton on 2/23/2009 2:30:57 AM&lt;br /&gt;
&lt;p&gt;
&lt;p style="padding-left:30px;"&gt;We are family&amp;nbsp;&lt;br /&gt;I got all my sisters with me&amp;nbsp;&lt;br /&gt;Sister Sledge&lt;/p&gt;
&lt;p&gt;Some things belong together, like Apple Pie and Ice Cream, or Sonny and Cher. And so it is with [[Entity|Entities]] and [[Value Object|Value Objects (VOs)]] &amp;ndash; some of them belong together.&lt;/p&gt;
&lt;p&gt;[[Aggregate|Aggregates]] are groups of things that belong together. An [[Aggregate Root]] is the thing that holds them all together.&lt;/p&gt;
&lt;p&gt;I will warn in advance, as I proof read this post, it was pretty complicated &amp;ndash; while I have tried to simplify the concepts, I am not certain I have totally succeeded. Hopefully [[Aggregate|Aggregates]] will become more clear later in the series when I start exploring them in code.&lt;/p&gt;
&lt;h3&gt;A Simple Example&lt;/h3&gt;
&lt;p&gt;In all Object Oriented programming we are used to dealing with objects that have references to other objects, and in DDD it is no different. For example, our Customer may have a reference to the customer&amp;rsquo;s Orders.&lt;/p&gt;
&lt;p&gt;An [[Aggregate]] is slightly different, where as Customers and Orders can exist in the system independently, some [[Entity|Entities]] and [[Value Object|Value Objects]] make absolutely no sense without their parent. The obvious example following on is Orders and OrderLines.&lt;/p&gt;
&lt;p&gt;OrderLines have no reason to exist without their parent Order, nor can they belong to any other Order. In this case, Order and OrderLines would probably be an [[Aggregate]], and the Order would be the [[Aggregate Root]]&lt;/p&gt;
&lt;p&gt;The rule of Cascading Delete is sometimes cited as a good way to tell if you have a group of [[Entity|Entities]] or [[Value Object|VOs]] that should be an [[Aggregate]] &amp;ndash; if the parent, in this case the Order, was deleted all other parts of that [[Aggregate]] below Order would be deleted too. So if it doesn&amp;rsquo;t make sense that a parent being deleted would also delete all children, then you don&amp;rsquo;t have an [[Aggregate]], you just have a good old fashioned reference.&lt;/p&gt;
&lt;h3&gt;So What is the Point of an Aggregate?&lt;/h3&gt;
&lt;p&gt;Well the first and most obvious point of an [[Aggregate]] is that it operates to a large degree as one &amp;ldquo;thing&amp;rdquo;. Notably the [[Aggregate Root]] is the single [[Entity]] that controls access to the children.&lt;/p&gt;
&lt;p&gt;Where you may have a Customer with operations like .UpdateToPreferredStatus and you may have an Order with .GetSumTotal &amp;ndash; the OrderLines of an Order would not have any logic exposed outside of the Order [[Entity]] &amp;ndash; in other words to add a new OrderLine, or to change an OrderLine, you would tell the Order to make the changes &amp;ndash; Order.AddNewItem for example.&lt;/p&gt;
&lt;p&gt;In this respect, [[Aggregate|Aggregates]] provide a clean pattern to keep logic where it really belongs.&lt;/p&gt;
&lt;p&gt;Another aspect of [[Aggregate Root|Aggregate Roots]] is that they are the [[Entity|Entities]] that are dealt with by [[Repository|Repositories]].&lt;/p&gt;
&lt;p&gt;In our examples above, we would have a Customer [[Repository]], and an Order [[Repository]], but there would not be an OrderLine [[Repository]]. It is the responsibility of the [[Repository]] for the [[Aggregate Root]] to deal with persistence of all the children of that [[Aggregate]].&lt;/p&gt;
&lt;h3&gt;Restrictions on Aggregates and Aggregate Roots&lt;/h3&gt;
&lt;p&gt;The main, and possibly obvious restriction on Aggregate Roots is, they must be Entities, and cannot be Value Objects. Back to the previous post, you will remember that [[Entity|Entities]] have Identity, and [[Value Object|Value Objects]] do not &amp;ndash; you could not ask a [[Repository]] to retrieve an [[Aggregate Root]] if it had no Identity.&lt;/p&gt;
&lt;p&gt;Within an [[Aggregate]], the other players can be&amp;nbsp;[[Entity|Entities]] and [[Value Objects|VOs]]&amp;nbsp;as the [[Domain]] dictates. For example, expanding our Order example further, the [[Aggregate]] may comprise the Order ([[Aggregate Root]]), the Order may have an OrderNumber ([[Value Object]]), some OrderLines ([[Entity|Entities]]), and a Shipping Address and Billing Address ([[Value Object|Value Objects]])&lt;/p&gt;
&lt;p&gt;Entities can hold references to any [[Aggregate Root]], but never to any other [[Entity]] or [[Value Object|VO]] within the&amp;nbsp;[[Aggregate|Aggregates]]. To access any other part of the&amp;nbsp;[[Aggregate]], you must navigate from the [[Aggregate Root]].&lt;/p&gt;
&lt;p&gt;How the component parts of an&amp;nbsp;[[Aggregate|Aggregates]]&amp;nbsp;are persisted is a matter for the implementation behind [[Repository]], but if you were using an ORM like NHibernate for example, the changes are that the [[Value Object|Value Objects]] would be NHibernate Components nested in their parent entity record and the [[Entity|Entities]] would be old fashioned mappings, each with their own table.&lt;/p&gt;
&lt;h3&gt;In Conclusion&lt;/h3&gt;
&lt;p&gt;[[Aggregate|Aggregates]] provide a logical grouping of [[Entity|Entities]] and [[Value Object|Value Objects]] that belong together at all times. An [[Aggregate Root]] is the gatekeeper to the [[Aggregate]]. Each [[Aggregate]] is treated as a single unit for persistence purposes.&lt;/p&gt;
&lt;p&gt;By logically grouping [[Entity|Entities]] and [[Value Objects|VOs]] in this way, we provide a mechanism to strictly manage a grouping of objects, and a way to allow us to treat a number of different&amp;nbsp;[[Entity|Entities]] and [[Value Objects|VOs]]&amp;nbsp;as one.&lt;/p&gt;
&lt;/p&gt;</description></item></channel></rss>