<?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: Refactoring Day 1 : Encapsulate Collection</title>
	<atom:link href="http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/</link>
	<description>Just another LosTechies site</description>
	<lastBuildDate>Thu, 13 Sep 2012 07:11:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: zhang hua</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-368</link>
		<dc:creator>zhang hua</dc:creator>
		<pubDate>Sat, 16 Oct 2010 12:45:45 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-368</guid>
		<description>it is very important to me  thanks  my english is very poor.</description>
		<content:encoded><![CDATA[<p>it is very important to me  thanks  my english is very poor.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mbrown77@gmail.com</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-366</link>
		<dc:creator>mbrown77@gmail.com</dc:creator>
		<pubDate>Tue, 01 Sep 2009 14:59:08 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-366</guid>
		<description>The trick to preventing the user from casting back to a List is to just explicitly return an enumerator

public IEnumerable&lt;OrderLine&gt; OrderLines 
{  
   get { return _orderLines.Select(o=&gt;o); }
}

Will return only the IEnumerable and casting will return an InvalidCastException. Also, you get the advantage of deferred enumeration. Loading the Enumerable into a ReadOnlyCollection, immediately iterates over the entire list.</description>
		<content:encoded><![CDATA[<p>The trick to preventing the user from casting back to a List is to just explicitly return an enumerator</p>
<p>public IEnumerable<orderline> OrderLines<br />
{<br />
   get { return _orderLines.Select(o=>o); }<br />
}</p>
<p>Will return only the IEnumerable and casting will return an InvalidCastException. Also, you get the advantage of deferred enumeration. Loading the Enumerable into a ReadOnlyCollection, immediately iterates over the entire list.</orderline></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JeromeQc</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-365</link>
		<dc:creator>JeromeQc</dc:creator>
		<pubDate>Wed, 05 Aug 2009 00:14:38 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-365</guid>
		<description>I came around to see if I got a reply and I realized the order total is not really encapsulated because any imprudent developer reusing this object, not aware of the implementation, could decide to move the lines in some other data structure that is more handy and the order total wouldn&#039;t add up anymore. I think implementing INotifyPropertyChanged would be a much better approach.

Here&#039;s the test I wrote just to make sure I wasn&#039;t in the threes:

[TestFixture]
	public class OrderTests
	{		
		[Test]
		public void OrderTotalIsNotEncapsulated()
		{
			var order = new Order();
			var orderLines = new List&lt;OrderLine&gt;(order.OrderLines);
			
			orderLines.Add(new OrderLine(1));
			Assert.AreEqual(0, order.Total);
		}
	}
	
	class Order
	{
		private List&lt;OrderLine&gt; _orderLines = new List&lt;OrderLine&gt;();
		private double _orderTotal;
	
	 	public IEnumerable&lt;OrderLine&gt; OrderLines
	    {
	        get { return _orderLines; }
		}
		
		public double Total { get { return _orderTotal; } }
	
		public void AddOrderLine(OrderLine orderLine)
		{
			_orderTotal += orderLine.Total;
			_orderLines.Add(orderLine);
		}
		
		public void RemoveOrderLine(OrderLine orderLine)
	 	{
			orderLine = _orderLines.Find(o =&gt; o == orderLine);
	
	        if (orderLine == null) 
				return;
	
	  		_orderTotal -= orderLine.Total;
			_orderLines.Remove(orderLine);
		}
	}
	
	class OrderLine
	{			
		public OrderLine(double total)
		{
			Total = total;
		}
		
		public double Total { get; private set; }
	}</description>
		<content:encoded><![CDATA[<p>I came around to see if I got a reply and I realized the order total is not really encapsulated because any imprudent developer reusing this object, not aware of the implementation, could decide to move the lines in some other data structure that is more handy and the order total wouldn&#8217;t add up anymore. I think implementing INotifyPropertyChanged would be a much better approach.</p>
<p>Here&#8217;s the test I wrote just to make sure I wasn&#8217;t in the threes:</p>
<p>[TestFixture]<br />
	public class OrderTests<br />
	{<br />
		[Test]<br />
		public void OrderTotalIsNotEncapsulated()<br />
		{<br />
			var order = new Order();<br />
			var orderLines = new List<orderline>(order.OrderLines);</p>
<p>			orderLines.Add(new OrderLine(1));<br />
			Assert.AreEqual(0, order.Total);<br />
		}<br />
	}</p>
<p>	class Order<br />
	{<br />
		private List</orderline><orderline> _orderLines = new List</orderline><orderline>();<br />
		private double _orderTotal;</p>
<p>	 	public IEnumerable</orderline><orderline> OrderLines<br />
	    {<br />
	        get { return _orderLines; }<br />
		}</p>
<p>		public double Total { get { return _orderTotal; } }</p>
<p>		public void AddOrderLine(OrderLine orderLine)<br />
		{<br />
			_orderTotal += orderLine.Total;<br />
			_orderLines.Add(orderLine);<br />
		}</p>
<p>		public void RemoveOrderLine(OrderLine orderLine)<br />
	 	{<br />
			orderLine = _orderLines.Find(o => o == orderLine);</p>
<p>	        if (orderLine == null)<br />
				return;</p>
<p>	  		_orderTotal -= orderLine.Total;<br />
			_orderLines.Remove(orderLine);<br />
		}<br />
	}</p>
<p>	class OrderLine<br />
	{<br />
		public OrderLine(double total)<br />
		{<br />
			Total = total;<br />
		}</p>
<p>		public double Total { get; private set; }<br />
	}</orderline></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-364</link>
		<dc:creator>Josh</dc:creator>
		<pubDate>Tue, 04 Aug 2009 23:23:35 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-364</guid>
		<description>I remember when I first wanted to limit functionality of a list and being dismayed at having to write such additional code, but such is life. Thanks for the clear example.</description>
		<content:encoded><![CDATA[<p>I remember when I first wanted to limit functionality of a list and being dismayed at having to write such additional code, but such is life. Thanks for the clear example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JeromeQc</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-363</link>
		<dc:creator>JeromeQc</dc:creator>
		<pubDate>Tue, 04 Aug 2009 16:49:05 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-363</guid>
		<description>Oups, sorry for the duplicate, this is against the DRY principle ; )</description>
		<content:encoded><![CDATA[<p>Oups, sorry for the duplicate, this is against the DRY principle ; )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JeromeQc</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-362</link>
		<dc:creator>JeromeQc</dc:creator>
		<pubDate>Tue, 04 Aug 2009 16:38:47 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-362</guid>
		<description>I this case I would prefer to have another object to encapsulate access the collection and subscribe to its events instead of adding logic in the collection itself.

It think this breaks the Single Responsibility Principle. I would rather have an OrderService to handle the business logic and add up the total. This way the order collection would be lighter to carry around or reuse on other layers. I personally don&#039;t like to embed business logic into entities (or their collection) to keep the domain simpler.

I like to see entities as simple documents an nothing more. But of course this approach is still good and I guess it is just a personal choice.

Here&#039;s a quote from Domain Driven Design Quickly: &quot;The verbs of the language, associated with their corresponding nouns become the part of the behavior of those objects. But there are some actions in the domain, some verbs, which do not seem to belong to any object. They represent an important behavior of the domain, so they cannot be neglected or simply incorporated into some of the Entities or Value Objects. Adding such behavior to an object would spoil the object, making it stand for functionality which does not belong to it. Nonetheless, using an object-oriented language, we have to use an object for this purpose. We can’t just have a separate function on its own. It has to be attached to some object. Often this kind of behavior functions across several objects, perhaps of different classes. For example, to transfer money from one account to another; should that function be in the sending account or the receiving account? It feels just as misplaced in either.&quot;

I am not a design guru so I might be taking this principle a bit too far. What do you think about it?

I really enjoy your posts, I came across your blog before and I think you are a good good writer. I will follow this series for sure!</description>
		<content:encoded><![CDATA[<p>I this case I would prefer to have another object to encapsulate access the collection and subscribe to its events instead of adding logic in the collection itself.</p>
<p>It think this breaks the Single Responsibility Principle. I would rather have an OrderService to handle the business logic and add up the total. This way the order collection would be lighter to carry around or reuse on other layers. I personally don&#8217;t like to embed business logic into entities (or their collection) to keep the domain simpler.</p>
<p>I like to see entities as simple documents an nothing more. But of course this approach is still good and I guess it is just a personal choice.</p>
<p>Here&#8217;s a quote from Domain Driven Design Quickly: &#8220;The verbs of the language, associated with their corresponding nouns become the part of the behavior of those objects. But there are some actions in the domain, some verbs, which do not seem to belong to any object. They represent an important behavior of the domain, so they cannot be neglected or simply incorporated into some of the Entities or Value Objects. Adding such behavior to an object would spoil the object, making it stand for functionality which does not belong to it. Nonetheless, using an object-oriented language, we have to use an object for this purpose. We can’t just have a separate function on its own. It has to be attached to some object. Often this kind of behavior functions across several objects, perhaps of different classes. For example, to transfer money from one account to another; should that function be in the sending account or the receiving account? It feels just as misplaced in either.&#8221;</p>
<p>I am not a design guru so I might be taking this principle a bit too far. What do you think about it?</p>
<p>I really enjoy your posts, I came across your blog before and I think you are a good good writer. I will follow this series for sure!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JeromeQc</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-361</link>
		<dc:creator>JeromeQc</dc:creator>
		<pubDate>Tue, 04 Aug 2009 16:36:26 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-361</guid>
		<description>I this case I would prefer to have another object to encapsulate access the collection and subscribe to its events instead of adding logic in the collection itself.

It think this breaks the Single Responsibility Principle. I would rather have an OrderService to handle the business logic and add up the total. This way the order collection would be lighter to carry around or reuse on other layers. I personally don&#039;t like to embed business logic into entities (or their collection) to keep the domain simpler.

I like to see entities as simple documents an nothing more. But of course this approach is still good and I guess it is just a personal choice.

Here&#039;s a quote from Domain Driven Design Quickly: &quot;The verbs of the language, associated with their corresponding nouns become the part of the behavior of those objects. But there are some actions in the domain, some verbs, which do not seem to belong to any object. They represent an important behavior of the domain, so they cannot be neglected or simply incorporated into some of the Entities or Value Objects. Adding such behavior to an object would spoil the object, making it stand for functionality which does not belong to it. Nonetheless, using an object-oriented language, we have to use an object for this purpose. We can’t just have a separate function on its own. It has to be attached to some object. Often this kind of behavior functions across several objects, perhaps of different classes. For example, to transfer money from one account to another; should that function be in the sending account or the receiving account? It feels just as misplaced in either.&quot;

I am not a design guru so I might be taking this principle a bit too far. What do you think about it?

I really enjoy your posts, I came across your blog before and I think you are a good good writer. I will follow this series for sure!</description>
		<content:encoded><![CDATA[<p>I this case I would prefer to have another object to encapsulate access the collection and subscribe to its events instead of adding logic in the collection itself.</p>
<p>It think this breaks the Single Responsibility Principle. I would rather have an OrderService to handle the business logic and add up the total. This way the order collection would be lighter to carry around or reuse on other layers. I personally don&#8217;t like to embed business logic into entities (or their collection) to keep the domain simpler.</p>
<p>I like to see entities as simple documents an nothing more. But of course this approach is still good and I guess it is just a personal choice.</p>
<p>Here&#8217;s a quote from Domain Driven Design Quickly: &#8220;The verbs of the language, associated with their corresponding nouns become the part of the behavior of those objects. But there are some actions in the domain, some verbs, which do not seem to belong to any object. They represent an important behavior of the domain, so they cannot be neglected or simply incorporated into some of the Entities or Value Objects. Adding such behavior to an object would spoil the object, making it stand for functionality which does not belong to it. Nonetheless, using an object-oriented language, we have to use an object for this purpose. We can’t just have a separate function on its own. It has to be attached to some object. Often this kind of behavior functions across several objects, perhaps of different classes. For example, to transfer money from one account to another; should that function be in the sending account or the receiving account? It feels just as misplaced in either.&#8221;</p>
<p>I am not a design guru so I might be taking this principle a bit too far. What do you think about it?</p>
<p>I really enjoy your posts, I came across your blog before and I think you are a good good writer. I will follow this series for sure!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simone</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-360</link>
		<dc:creator>Simone</dc:creator>
		<pubDate>Tue, 04 Aug 2009 08:00:57 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-360</guid>
		<description>Hey Sean,
are you thinking about putting all the refactorings into a kind of eBook or at least providing them all into a PDF as the DDD series of post was done at the beginning of this year?
It would be a great reference... expecially since I&#039;ll be on vacation from tomorrow till almost the end of August, and I&#039;ll miss them all :)
Simo</description>
		<content:encoded><![CDATA[<p>Hey Sean,<br />
are you thinking about putting all the refactorings into a kind of eBook or at least providing them all into a PDF as the DDD series of post was done at the beginning of this year?<br />
It would be a great reference&#8230; expecially since I&#8217;ll be on vacation from tomorrow till almost the end of August, and I&#8217;ll miss them all <img src='http://lostechies.com/seanchambers/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Simo</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-359</link>
		<dc:creator>Erik</dc:creator>
		<pubDate>Tue, 04 Aug 2009 01:11:50 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-359</guid>
		<description>Stephen,

 Thanks I was not aware of what the events args were of OnCollectionChanged.  That said I still find the contract of the aggregate to be much cleaner and more explicit with the Add Remove methods.  In fact if this were in my Domain I would probably even new up the OrderLine Item as part of the method instead of having it passed in as an argument.  

My Aggregate might start like
http://www.codepaste.net/kho2qm

and then  later evolve in many different directions including moving to a write only model or adding back references on your internal representation. 

http://www.codepaste.net/n1no9o

It seems to me that this is a case where by being explicit in your contract, you can evolve many more directions while still maintaining the same external contract than what you gain by violating Dementer (I know there are often good reasons to violate Dementer, but I am not sure what they are here.)  What is it we gain by exposing and observing the collection.  I started down that path once and wrote a few list Decorators that provided On Insert, After Insert, etc... to Existing ILists&lt;T&gt;.  

The problem is that decorating the list and wiring up your event handlers was not much if any less code and when your object evolved and you wanted to change your internal representation of the collection(like to a &quot;write only model&quot; like above), you had to change all your calling code or write a bunch of biderectional synchronization code and events.

Often times 1 refactoring can lead to another.  You might make the first refactoring above and then you think Why do my callers have to know how to construct an orderline.  Let&#039;s fix all the places I am newing up an orderline before this gets out of hand.

The calling code goes from 

var order=OrderProvider.GetOrder(OrderID);
var orderline = new orderline (Order,itemType,qty);
order.Orderlines.Add(orderline);

to

OrderProvider.GetOrder(OrderID).AddItems(itemType,qty);	

and has fewer reasons to change.  To me this is a good thing.

Thanks,

Erik

</description>
		<content:encoded><![CDATA[<p>Stephen,</p>
<p> Thanks I was not aware of what the events args were of OnCollectionChanged.  That said I still find the contract of the aggregate to be much cleaner and more explicit with the Add Remove methods.  In fact if this were in my Domain I would probably even new up the OrderLine Item as part of the method instead of having it passed in as an argument.  </p>
<p>My Aggregate might start like<br />
<a href="http://www.codepaste.net/kho2qm" rel="nofollow">http://www.codepaste.net/kho2qm</a></p>
<p>and then  later evolve in many different directions including moving to a write only model or adding back references on your internal representation. </p>
<p><a href="http://www.codepaste.net/n1no9o" rel="nofollow">http://www.codepaste.net/n1no9o</a></p>
<p>It seems to me that this is a case where by being explicit in your contract, you can evolve many more directions while still maintaining the same external contract than what you gain by violating Dementer (I know there are often good reasons to violate Dementer, but I am not sure what they are here.)  What is it we gain by exposing and observing the collection.  I started down that path once and wrote a few list Decorators that provided On Insert, After Insert, etc&#8230; to Existing ILists<t>.  </p>
<p>The problem is that decorating the list and wiring up your event handlers was not much if any less code and when your object evolved and you wanted to change your internal representation of the collection(like to a &#8220;write only model&#8221; like above), you had to change all your calling code or write a bunch of biderectional synchronization code and events.</p>
<p>Often times 1 refactoring can lead to another.  You might make the first refactoring above and then you think Why do my callers have to know how to construct an orderline.  Let&#8217;s fix all the places I am newing up an orderline before this gets out of hand.</p>
<p>The calling code goes from </p>
<p>var order=OrderProvider.GetOrder(OrderID);<br />
var orderline = new orderline (Order,itemType,qty);<br />
order.Orderlines.Add(orderline);</p>
<p>to</p>
<p>OrderProvider.GetOrder(OrderID).AddItems(itemType,qty);	</p>
<p>and has fewer reasons to change.  To me this is a good thing.</p>
<p>Thanks,</p>
<p>Erik</p>
<p></t></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen</title>
		<link>http://lostechies.com/seanchambers/2009/08/02/refactoring-day-1-encapsulate-collection/#comment-358</link>
		<dc:creator>Stephen</dc:creator>
		<pubDate>Mon, 03 Aug 2009 19:13:36 +0000</pubDate>
		<guid isPermaLink="false">/blogs/sean_chambers/archive/2009/08/02/refactoring-day-1-encapsulate-collection.aspx#comment-358</guid>
		<description>@Erik, theres no reason you can&#039;t have a list implementation where you can inject policy.. the whole point being the caller doesn&#039;t have to implicitely include the aggregate by calling it rather than the &#039;collection&#039; you would normally with any old .net object.

And theres no digging, the collection simply pushes observable events when items are removed or added.. and no you wouldn&#039;t need to enumerate the list, you get an enumerable of added / removed items in the event args (to support mass adds / removes).

Remembering the main reason these methods exist AddX vs X&#039;s.Add is purely by a limitation that there weren&#039;t great ways to add policy or observation in from the aggregate.. it isn&#039;t something thats desirable..</description>
		<content:encoded><![CDATA[<p>@Erik, theres no reason you can&#8217;t have a list implementation where you can inject policy.. the whole point being the caller doesn&#8217;t have to implicitely include the aggregate by calling it rather than the &#8216;collection&#8217; you would normally with any old .net object.</p>
<p>And theres no digging, the collection simply pushes observable events when items are removed or added.. and no you wouldn&#8217;t need to enumerate the list, you get an enumerable of added / removed items in the event args (to support mass adds / removes).</p>
<p>Remembering the main reason these methods exist AddX vs X&#8217;s.Add is purely by a limitation that there weren&#8217;t great ways to add policy or observation in from the aggregate.. it isn&#8217;t something thats desirable..</p>
]]></content:encoded>
	</item>
</channel>
</rss>
