<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Value objects: Immutability and Equality</title>
	<atom:link href="http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Sat, 11 Feb 2012 23:17:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25418</link>
		<dc:creator>Mark Needham</dc:creator>
		<pubDate>Mon, 26 Oct 2009 21:36:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25418</guid>
		<description>@Ron - yeah sorry I should have mentioned that on the post. 

The implementation did override those as well but I didn&#039;t show the code for that bit cause it&#039;s quite verbose! Good spot!

@alberto - we were able to access them because &#039;obj&#039; refers to the class that we are currently in. 

Thanks for the link to the post, looks interesting.</description>
		<content:encoded><![CDATA[<p>@Ron &#8211; yeah sorry I should have mentioned that on the post. </p>
<p>The implementation did override those as well but I didn&#8217;t show the code for that bit cause it&#8217;s quite verbose! Good spot!</p>
<p>@alberto &#8211; we were able to access them because &#8216;obj&#8217; refers to the class that we are currently in. </p>
<p>Thanks for the link to the post, looks interesting.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ron</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25397</link>
		<dc:creator>Ron</dc:creator>
		<pubDate>Mon, 26 Oct 2009 17:20:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25397</guid>
		<description>Should MyValueObject also override Equals(object) and GetHashCode()? Imagine a case where your MyValueObject gets cast to &#039;object&#039; and then Equals gets called on it or it gets placed in a Dictionary. You would want your custom Equals(MyValueObject) to be called, right?

public override bool Equals(object obj)
{
  if (obj is MyValueObject)
    return Equals((MyValueObject)obj);
  return false;
}

public override int GetHashCode()
{
  return
    otherValue.GetHashCode() ^
    someValue.GetHashCode() ^
    someOtherValue.GetHashCode();
}</description>
		<content:encoded><![CDATA[<p>Should MyValueObject also override Equals(object) and GetHashCode()? Imagine a case where your MyValueObject gets cast to &#8216;object&#8217; and then Equals gets called on it or it gets placed in a Dictionary. You would want your custom Equals(MyValueObject) to be called, right?</p>
<p>public override bool Equals(object obj)<br />
{<br />
  if (obj is MyValueObject)<br />
    return Equals((MyValueObject)obj);<br />
  return false;<br />
}</p>
<p>public override int GetHashCode()<br />
{<br />
  return<br />
    otherValue.GetHashCode() ^<br />
    someValue.GetHashCode() ^<br />
    someOtherValue.GetHashCode();<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alberto</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25390</link>
		<dc:creator>alberto</dc:creator>
		<pubDate>Mon, 26 Oct 2009 16:17:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25390</guid>
		<description>You cannot access the fields of obj to do the comparison, they are private.

&lt;a href=&quot;Here&quot; rel=&quot;nofollow&quot;&gt;http://grabbagoft.blogspot.com/2007/06/generic-value-object-equality.html&lt;/a&gt; is a good generic implementation.</description>
		<content:encoded><![CDATA[<p>You cannot access the fields of obj to do the comparison, they are private.</p>
<p><a href="Here" rel="nofollow">http://grabbagoft.blogspot.com/2007/06/generic-value-object-equality.html</a> is a good generic implementation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25368</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Mon, 26 Oct 2009 13:22:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25368</guid>
		<description>Your solution works fine if you only have a few parameters.  If you want to add more parameters you change the signature of the method.  This then causes you to either refactor  (if all the parameters are  required) or to overload the constructor, whereas if you just passed in the object changes would be minimal.  Maybe a better solution is to have a static method on your SomeMutableEntity to return your value object.   In the end the situation is going to dictate what you are going to do.</description>
		<content:encoded><![CDATA[<p>Your solution works fine if you only have a few parameters.  If you want to add more parameters you change the signature of the method.  This then causes you to either refactor  (if all the parameters are  required) or to overload the constructor, whereas if you just passed in the object changes would be minimal.  Maybe a better solution is to have a static method on your SomeMutableEntity to return your value object.   In the end the situation is going to dictate what you are going to do.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DotNetShoutout</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25362</link>
		<dc:creator>DotNetShoutout</dc:creator>
		<pubDate>Mon, 26 Oct 2009 12:18:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25362</guid>
		<description>&lt;strong&gt;Value objects: Immutability and Equality - Mark Needham...&lt;/strong&gt;

Thank you for submitting this cool story - Trackback from DotNetShoutout...</description>
		<content:encoded><![CDATA[<p><strong>Value objects: Immutability and Equality &#8211; Mark Needham&#8230;</strong></p>
<p>Thank you for submitting this cool story &#8211; Trackback from DotNetShoutout&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25352</link>
		<dc:creator>Mark Needham</dc:creator>
		<pubDate>Mon, 26 Oct 2009 10:45:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25352</guid>
		<description>@Chuck - yeah I guess if it had been called SomeMutableEntity in our code I probably wouldn&#039;t have messed it up! 

Thought I&#039;d use that name just to make it obvious why the first solution doesn&#039;t make sense.</description>
		<content:encoded><![CDATA[<p>@Chuck &#8211; yeah I guess if it had been called SomeMutableEntity in our code I probably wouldn&#8217;t have messed it up! </p>
<p>Thought I&#8217;d use that name just to make it obvious why the first solution doesn&#8217;t make sense.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chuck</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25351</link>
		<dc:creator>Chuck</dc:creator>
		<pubDate>Mon, 26 Oct 2009 10:35:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25351</guid>
		<description>Mark - I liked your final solution. Red Alerts rang out when SomeMutableEntity was passed in. You touched on it: the Client doesn&#039;t really know that you are not (internally) using SomeMutableEntity for some additional processing (oh, and what Dave said).

Thanks for the post!

C</description>
		<content:encoded><![CDATA[<p>Mark &#8211; I liked your final solution. Red Alerts rang out when SomeMutableEntity was passed in. You touched on it: the Client doesn&#8217;t really know that you are not (internally) using SomeMutableEntity for some additional processing (oh, and what Dave said).</p>
<p>Thanks for the post!</p>
<p>C</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reflective Perspective - Chris Alcock &#187; The Morning Brew #462</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25336</link>
		<dc:creator>Reflective Perspective - Chris Alcock &#187; The Morning Brew #462</dc:creator>
		<pubDate>Mon, 26 Oct 2009 08:41:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25336</guid>
		<description>[...] Value objects: Immutability and Equality - Mark Needham explores the concept of the value object and immutability of such objects with a real world example along with some techniques to work around some of the difficult parts of the implementation [...]</description>
		<content:encoded><![CDATA[<p>[...] Value objects: Immutability and Equality &#8211; Mark Needham explores the concept of the value object and immutability of such objects with a real world example along with some techniques to work around some of the difficult parts of the implementation [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leonardo Borges</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25209</link>
		<dc:creator>Leonardo Borges</dc:creator>
		<pubDate>Sat, 24 Oct 2009 14:59:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25209</guid>
		<description>Hi Mark,
Yes you are correct. In Java I&#039;d probably use the clone method like Chris mentioned.
Also, his point about collections is very well taken as it can lead to a false impression of immutability.</description>
		<content:encoded><![CDATA[<p>Hi Mark,<br />
Yes you are correct. In Java I&#8217;d probably use the clone method like Chris mentioned.<br />
Also, his point about collections is very well taken as it can lead to a false impression of immutability.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Leishman</title>
		<link>http://www.markhneedham.com/blog/2009/10/23/value-objects-immutability-and-equality/comment-page-1/#comment-25198</link>
		<dc:creator>Chris Leishman</dc:creator>
		<pubDate>Sat, 24 Oct 2009 07:21:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1752#comment-25198</guid>
		<description>Note that this is _really_ commonly missed when dealing with collections.  Although it&#039;s still not perfect, I usually do this (in java):

public Constructor(final List entities) {
    this.entities = new ArrayList(entities);
}

However, you often also have a getter for this list.  So to be doubly sure nobody can modify the copy, I&#039;d do:

public Constructor(final List entities) {
    this.entities = Collections.unmodifiableList(new ArrayList(entities));
}</description>
		<content:encoded><![CDATA[<p>Note that this is _really_ commonly missed when dealing with collections.  Although it&#8217;s still not perfect, I usually do this (in java):</p>
<p>public Constructor(final List entities) {<br />
    this.entities = new ArrayList(entities);<br />
}</p>
<p>However, you often also have a getter for this list.  So to be doubly sure nobody can modify the copy, I&#8217;d do:</p>
<p>public Constructor(final List entities) {<br />
    this.entities = Collections.unmodifiableList(new ArrayList(entities));<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

