<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>mhinze.com &#187; value objects</title>
	<atom:link href="http://mhinze.com/tag/value-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://mhinze.com</link>
	<description>Matt Hinze, learning in public</description>
	<lastBuildDate>Fri, 18 May 2012 15:09:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mhinze.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>mhinze.com &#187; value objects</title>
		<link>http://mhinze.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mhinze.com/osd.xml" title="mhinze.com" />
	<atom:link rel='hub' href='http://mhinze.com/?pushpress=hub'/>
		<item>
		<title>More On Value Objects</title>
		<link>http://mhinze.com/2009/02/19/more-on-value-objects/</link>
		<comments>http://mhinze.com/2009/02/19/more-on-value-objects/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 23:57:52 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Domain-Driven Design]]></category>
		<category><![CDATA[ddd]]></category>
		<category><![CDATA[value objects]]></category>

		<guid isPermaLink="false">http://mhinze.com/more-on-value-objects/</guid>
		<description><![CDATA[A few days ago Dylan Beattie wrote a nice post about value objects.&#160; He explains the idea in a more palatable way than my attempt: If it’s not clear how to model a particular element in your model, try asking &#8230; <a href="http://mhinze.com/2009/02/19/more-on-value-objects/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mhinze.com&#038;blog=678824&#038;post=114&#038;subd=mhinze&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A few days ago Dylan Beattie wrote a nice <a href="http://dylanbeattie.blogspot.com/2009/02/is-crisp-value-object.html">post about value objects</a>.&nbsp; He explains the idea in a more <em>palatable</em> way than <a href="http://mhinze.com/there-is-never-a-collection-of-value-objects/">my attempt</a>:</p>
<blockquote><p>If it’s not clear how to model a particular element in your model, try asking “which one?” If the question makes sense within your own scenario then you’re probably dealing with entities. If the question “which one” is meaningless <strong>in the context of your domain </strong>then you’re probably better off modeling the subject of the question as a value object.</p>
</blockquote>
<p> I suggest that if you&#8217;re <em>maintaining</em> a collection, you implicitly care <em>which one</em>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mhinze.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mhinze.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mhinze.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mhinze.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mhinze.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mhinze.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mhinze.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mhinze.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mhinze.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mhinze.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mhinze.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mhinze.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mhinze.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mhinze.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mhinze.com&#038;blog=678824&#038;post=114&#038;subd=mhinze&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mhinze.com/2009/02/19/more-on-value-objects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d6043840ea98542a9c9331c7e7940b1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhinze</media:title>
		</media:content>
	</item>
		<item>
		<title>There is never a collection of Value Objects</title>
		<link>http://mhinze.com/2009/01/10/there-is-never-a-collection-of-value-objects/</link>
		<comments>http://mhinze.com/2009/01/10/there-is-never-a-collection-of-value-objects/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 05:59:57 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Domain-Driven Design]]></category>
		<category><![CDATA[ddd]]></category>
		<category><![CDATA[value objects]]></category>

		<guid isPermaLink="false">http://mhinze.com/there-is-never-a-collection-of-value-objects/</guid>
		<description><![CDATA[Adding to a Value Object property always adds to it, never adds another instance to it.&#160; Distinguishing Value Objects by maintaining a collection of them implies identity. It is a mistake to attempt to maintain a collection of Value Objects. &#8230; <a href="http://mhinze.com/2009/01/10/there-is-never-a-collection-of-value-objects/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mhinze.com&#038;blog=678824&#038;post=110&#038;subd=mhinze&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Adding to a Value Object property always <em>adds to it</em>, <strong>never</strong> <em>adds another instance to it</em>.&nbsp; Distinguishing Value Objects by maintaining a collection of them <strong>implies identity</strong>. </p>
<p>It is a mistake to attempt to maintain a collection of Value Objects.</p>
<p>Let&#8217;s take the near-canonical Address, a perfectly valid Value Object as described on page 98 of the Big Blue Bible:</p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">Address </span>: <span style="color:#2b91af;">ValueObject</span>&lt;<span style="color:#2b91af;">Address</span>&gt;
{
    <span style="color:blue;">public virtual string </span>StreetAddress1 { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public virtual string </span>StreetAddress2 { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public virtual string </span>City { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public virtual </span><span style="color:#2b91af;">State </span>State { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public virtual string </span>ZipCode { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public virtual </span><span style="color:#2b91af;">Country </span>Country { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
}</pre>
<p>And a perfectly valid usage of this Address Value Object is as a component of an Entity: </p>
<p><a href="http://11011.net/software/vspaste"></a>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">Customer </span>: <span style="color:#2b91af;">Entity
</span>{
    <span style="color:blue;">public </span><span style="color:#2b91af;">Address </span>WorkAddress { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public </span><span style="color:#2b91af;">Address </span>HomeAddress { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:green;">// ...
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This, on the other hand, is incorrect:</p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">Customer </span>: <span style="color:#2b91af;">Entity<br /></span>{
    <span style="color:blue;">private readonly </span><span style="color:#2b91af;">ISet</span>&lt;<span style="color:#2b91af;">Address</span>&gt; _addresses = <span style="color:blue;">new </span><span style="color:#2b91af;">HashedSet</span>&lt;<span style="color:#2b91af;">Address</span>&gt;();

    <span style="color:blue;">public </span><span style="color:#2b91af;">Address</span>[] GetAddresses()
    {
        <span style="color:blue;">return </span>_addresses.ToArray();
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Again, distinguishing addresses by maintaining a collection of them <strong>implies identity</strong>. If you tried to edit one of the addresses respecting some immutable Value Object oriented thinking, you would clear the collection, find (somehow) the one you intended to edit, replace it along with all the others.&nbsp; This is not possible.&nbsp; When you only care about the values of an object, rather than a key, you cannot find the original one you intend to edit from an instance of an edited object.&nbsp; If you care about a key you have an Entity.</p>
<p>The original and the edited will never match. You might try to give the Value Object a database key so that it is easy to find.&nbsp; But if you do that, why again are you clearing all the rest and re-adding them?&nbsp; That doesn&#8217;t make sense &#8211; just edit it. </p>
<p>There&#8217;s another problem.&nbsp; In the &#8220;wallet&#8221; example you have paper money in your wallet, several pieces of paper.&nbsp; Your wallet is the Entity and the money is a Value Object.&nbsp; If you were to take some money out of your wallet, you would subtract from the Value Object, not remove an instance of the Value Object from a collection.&nbsp; You don&#8217;t care if you have five ones or one five, you just care that you have five.&nbsp; Value Objects never collect, they only aggregate.</p>
<p>If you care about each individual instance, you have an Entity.</p>
<p>The best way to model something like this is to use an Entity that has a Value Object component.&nbsp; If I am maintaining a collection of Addresses, I do actually care that each is different &#8211; the trick is to find out <strong>why</strong>.&nbsp; In my case I care because the Customer has an address history and I wish to maintain that history.&nbsp; I model that like this:</p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">HistoricalAddress </span>: <span style="color:#2b91af;">Entity
</span>{
    <span style="color:blue;">public </span><span style="color:#2b91af;">Address </span>Address { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public </span><span style="color:#2b91af;">DateTime</span>? Start { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public </span><span style="color:#2b91af;">DateTime</span>? End { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public </span><span style="color:#2b91af;">AddressType </span>Type { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>Now the HistoricalAddress Entity lives in the Customer Aggregate&#8230;</p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">Customer </span>: <span style="color:#2b91af;">Entity
</span>{
    <span style="color:blue;">private readonly </span><span style="color:#2b91af;">ISet</span>&lt;<span style="color:#2b91af;">HistoricalAddress</span>&gt; _addresses = <span style="color:blue;">new </span><span style="color:#2b91af;">HashedSet</span>&lt;<span style="color:#2b91af;">HistoricalAddress</span>&gt;();

    <span style="color:blue;">public </span><span style="color:#2b91af;">HistoricalAddress</span>[] GetAddresses()
    {
        <span style="color:blue;">return </span>_addresses.ToArray();
    }

    <span style="color:blue;">public </span><span style="color:#2b91af;">HistoricalAddress </span>GetAddress(<span style="color:#2b91af;">Guid </span>historicalAddressId)
    {
        <span style="color:blue;">return </span>_addresses.SingleOrDefault(x =&gt; x.Id == historicalAddressId);
    }

    <span style="color:green;">//...
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>&#8230; and I am free to edit, list, and delete them with ease.</p>
<p>The one caveat is when you are maintaining a collection of Value Objects that represent all possible values.&nbsp; If you have a State Value Object you may want to represent a collection of all 50 U.S. States as a select list in the user interface.&nbsp; This works fine and is programmatically possible, until you are interested in maintaining this collection.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mhinze.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mhinze.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mhinze.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mhinze.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mhinze.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mhinze.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mhinze.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mhinze.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mhinze.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mhinze.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mhinze.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mhinze.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mhinze.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mhinze.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mhinze.com&#038;blog=678824&#038;post=110&#038;subd=mhinze&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mhinze.com/2009/01/10/there-is-never-a-collection-of-value-objects/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d6043840ea98542a9c9331c7e7940b1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhinze</media:title>
		</media:content>
	</item>
	</channel>
</rss>
