<?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: Stop Exposing Collections Already!</title> <atom:link href="http://davybrion.com/blog/2009/10/stop-exposing-collections-already/feed/" rel="self" type="application/rss+xml" /><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/</link> <description>inquisitive: adjective. given to inquiry, research, or asking questions; eager for knowledge; intellectually curious</description> <lastBuildDate>Tue, 22 May 2012 17:40:00 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>By: Tom</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22865</link> <dc:creator>Tom</dc:creator> <pubDate>Sun, 01 Nov 2009 12:42:18 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22865</guid> <description>@Grant Palin: In my opinion, the code and time savings of using the XML serializer far outweigh the disadvantages.Instead of shunning List properties, it is smarter to instead get used to them.  In most cases they are harmless.  In my few years&#039; of .NET experience, I have yet to run into a problem such as having nefarious code modifying a list when it shouldn&#039;t.  I understand the argument against them, but I think it is somewhat academic.Now I wouldn&#039;t advocate putting a List property in a public API used by a lot of people, but using them in general code is okay in my opinion, especially when you are using XML serialization.If I were in your shoes, I would eliminate the duplication by removing the redundant interface property types.  You haven&#039;t gained anything by putting them in - after all, the lists can still be modified by the other property - and it messes up the code a lot.</description> <content:encoded><![CDATA[<p>@Grant Palin: In my opinion, the code and time savings of using the XML serializer far outweigh the disadvantages.</p><p>Instead of shunning List properties, it is smarter to instead get used to them.  In most cases they are harmless.  In my few years&#8217; of .NET experience, I have yet to run into a problem such as having nefarious code modifying a list when it shouldn&#8217;t.  I understand the argument against them, but I think it is somewhat academic.</p><p>Now I wouldn&#8217;t advocate putting a List property in a public API used by a lot of people, but using them in general code is okay in my opinion, especially when you are using XML serialization.</p><p>If I were in your shoes, I would eliminate the duplication by removing the redundant interface property types.  You haven&#8217;t gained anything by putting them in &#8211; after all, the lists can still be modified by the other property &#8211; and it messes up the code a lot.</p> ]]></content:encoded> </item> <item><title>By: Grant Palin</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22855</link> <dc:creator>Grant Palin</dc:creator> <pubDate>Thu, 29 Oct 2009 16:59:42 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22855</guid> <description>I agree with the principle, but there is a caveat. I have some code I would like to fix up, a class with a number of members. Some of those members are Lists of other types. The trick is, the main class, and the ones it uses, are persisted through XML serialization, which handles public members and/or properties. Collections may not be exposed via interfaces (e.g. IList) because the serialization will not work correctly. So at this point I have two properties for each collection member: one for consumption by code, and one for use by the serializer. Not an ideal situation.Any suggestions on how to remove the duplication?</description> <content:encoded><![CDATA[<p>I agree with the principle, but there is a caveat. I have some code I would like to fix up, a class with a number of members. Some of those members are Lists of other types. The trick is, the main class, and the ones it uses, are persisted through XML serialization, which handles public members and/or properties. Collections may not be exposed via interfaces (e.g. IList) because the serialization will not work correctly. So at this point I have two properties for each collection member: one for consumption by code, and one for use by the serializer. Not an ideal situation.</p><p>Any suggestions on how to remove the duplication?</p> ]]></content:encoded> </item> <item><title>By: herzmeister_der_welten</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22853</link> <dc:creator>herzmeister_der_welten</dc:creator> <pubDate>Thu, 29 Oct 2009 09:33:51 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22853</guid> <description>We need to use the System.ObjectModel.ReadOnlyCollection (or a custom read-only type for that matter) to wrap our inner collection either way. We can publish it as an IEnumerable if we like. But we must wrap it up, otherwise the caller obviously can cast it back to a List.</description> <content:encoded><![CDATA[<p>We need to use the System.ObjectModel.ReadOnlyCollection (or a custom read-only type for that matter) to wrap our inner collection either way. We can publish it as an IEnumerable if we like. But we must wrap it up, otherwise the caller obviously can cast it back to a List.</p> ]]></content:encoded> </item> <item><title>By: Morten Lyhr</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22847</link> <dc:creator>Morten Lyhr</dc:creator> <pubDate>Wed, 28 Oct 2009 19:13:18 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22847</guid> <description>I think this is another case of data on the inside vs. data on the outside.If I have to calculate the Sum of several order lines or validate the order lines, do the class need to expose its internal state?... not really.If I have a class that I need to expose to the UI, it does not really matter if it is a T[], IEnumerable, List, IList. Why ? Because the UI need to know the internals of the class in order to render it correctly.Perhaps your entity is responsible for more than one thing? Both being used by UI, where it is treated as data, and by other classes where it is used as and encapsulated object?Also when adding the AddXXX and RemoveXXX, you have all but said that your class is some sort of IList, why not implement the interface?</description> <content:encoded><![CDATA[<p>I think this is another case of data on the inside vs. data on the outside.</p><p>If I have to calculate the Sum of several order lines or validate the order lines, do the class need to expose its internal state?&#8230; not really.</p><p>If I have a class that I need to expose to the UI, it does not really matter if it is a T[], IEnumerable, List, IList. Why ? Because the UI need to know the internals of the class in order to render it correctly.</p><p>Perhaps your entity is responsible for more than one thing? Both being used by UI, where it is treated as data, and by other classes where it is used as and encapsulated object?</p><p>Also when adding the AddXXX and RemoveXXX, you have all but said that your class is some sort of IList, why not implement the interface?</p> ]]></content:encoded> </item> <item><title>By: Hack A Day</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22846</link> <dc:creator>Hack A Day</dc:creator> <pubDate>Wed, 28 Oct 2009 17:23:29 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22846</guid> <description>For the developer who thinks T[] is better, T[] is still mutable and doesn&#039;t solve the issue. All of the above is a matter of style, anyone who thinks &quot;my way is the best&quot; should be taken out in back a shot. Every situation is different, no one solution fits all cases. It&#039;s all a combination of easy-of-use, scalability and manageability. The best APIs are the ones that fit nicely for which they are intended, scale when needed and are easy to debug.</description> <content:encoded><![CDATA[<p>For the developer who thinks T[] is better, T[] is still mutable and doesn&#8217;t solve the issue. All of the above is a matter of style, anyone who thinks &#8220;my way is the best&#8221; should be taken out in back a shot. Every situation is different, no one solution fits all cases. It&#8217;s all a combination of easy-of-use, scalability and manageability. The best APIs are the ones that fit nicely for which they are intended, scale when needed and are easy to debug.</p> ]]></content:encoded> </item> <item><title>By: J.P. Hamilton</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22845</link> <dc:creator>J.P. Hamilton</dc:creator> <pubDate>Wed, 28 Oct 2009 14:06:19 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22845</guid> <description>@PhilipTo implement IList, don&#039;t you also need to implement ICollection as well as IEnumerable? That&#039;s about 13 methods. I can think of many scenarios where that is just too much behavior. For instance, inserting and removing elements at specific indexes. This is not only about preventing modifications to collections, but also about encapsulation, and the ISP to some degree.</description> <content:encoded><![CDATA[<p>@Philip</p><p>To implement IList, don&#8217;t you also need to implement ICollection as well as IEnumerable? That&#8217;s about 13 methods. I can think of many scenarios where that is just too much behavior. For instance, inserting and removing elements at specific indexes. This is not only about preventing modifications to collections, but also about encapsulation, and the ISP to some degree.</p> ]]></content:encoded> </item> <item><title>By: Awkward Coder</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22844</link> <dc:creator>Awkward Coder</dc:creator> <pubDate>Wed, 28 Oct 2009 13:41:47 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22844</guid> <description>@Dimitriy - you got a point there ;)@Philip  - may be there is more behaviour than just inserting into a list when AddWidget, RemoveWidget is called...</description> <content:encoded><![CDATA[<p>@Dimitriy &#8211; you got a point there <img
src='http://d18sni7re4ly7f.cloudfront.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>@Philip  &#8211; may be there is more behaviour than just inserting into a list when AddWidget, RemoveWidget is called&#8230;</p> ]]></content:encoded> </item> <item><title>By: Philip</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22843</link> <dc:creator>Philip</dc:creator> <pubDate>Wed, 28 Oct 2009 13:28:06 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22843</guid> <description>I see your argument for List.  But IList, ICollection?   If you&#039;re exposing a collection and providing your own Add/Remove methods, why wouldn&#039;t you simply make your class (or a collection class) implement IList?That&#039;s what the interfaces are there for, so that you can have complete control of the collection (like you want), but still allow people to use your collection in a standard and natural way.Personally, If I see a component that forces me to use AddWidget, GetIndex, RemoveWidget, etc., and doesn&#039;t implement IList I wonder if they designed the interface for me (the consumer) or for themselves.</description> <content:encoded><![CDATA[<p>I see your argument for List.  But IList, ICollection?   If you&#8217;re exposing a collection and providing your own Add/Remove methods, why wouldn&#8217;t you simply make your class (or a collection class) implement IList?</p><p>That&#8217;s what the interfaces are there for, so that you can have complete control of the collection (like you want), but still allow people to use your collection in a standard and natural way.</p><p>Personally, If I see a component that forces me to use AddWidget, GetIndex, RemoveWidget, etc., and doesn&#8217;t implement IList I wonder if they designed the interface for me (the consumer) or for themselves.</p> ]]></content:encoded> </item> <item><title>By: Dmitriy Naginryak</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22842</link> <dc:creator>Dmitriy Naginryak</dc:creator> <pubDate>Wed, 28 Oct 2009 13:11:22 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22842</guid> <description>@Awkward Coder,Having read-only objects?? Come on... You must have too much free time.There is only one framework that tracks all the possible changes: http://www.capableobjects.com/</description> <content:encoded><![CDATA[<p>@Awkward Coder,</p><p>Having read-only objects?? Come on&#8230; You must have too much free time.</p><p>There is only one framework that tracks all the possible changes: <a
href="http://www.capableobjects.com/" rel="nofollow">http://www.capableobjects.com/</a></p> ]]></content:encoded> </item> <item><title>By: Dmitriy Naginryak</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22841</link> <dc:creator>Dmitriy Naginryak</dc:creator> <pubDate>Wed, 28 Oct 2009 13:03:29 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22841</guid> <description>@Steve, ok, that is a poimnt.
But exposing IEnumerable there is no any guarantee at all about the actual implementation (IList, ICollection, IDictionary, whatever?) and we should know how to use the class&#039; interface - using Count() extension or some other way. Otherwise there is no point of abstraction and, as the post&#039;s idea is, in ability to replace the implementation.But that is not my main point. Points are:
1. You just cannot prevent possibility of side-effects only by exposing non-modifable collection.
2. It is bad to expose CLASS, instead INTERFACE should be used. I agrre that all List should be replaced by IList or similar but you cannot use IEnumarable as a guard for protecting collections/objects from modifications.Cheers.</description> <content:encoded><![CDATA[<p>@Steve, ok, that is a poimnt.<br
/> But exposing IEnumerable there is no any guarantee at all about the actual implementation (IList, ICollection, IDictionary, whatever?) and we should know how to use the class&#8217; interface &#8211; using Count() extension or some other way. Otherwise there is no point of abstraction and, as the post&#8217;s idea is, in ability to replace the implementation.</p><p>But that is not my main point. Points are:<br
/> 1. You just cannot prevent possibility of side-effects only by exposing non-modifable collection.<br
/> 2. It is bad to expose CLASS, instead INTERFACE should be used. I agrre that all List should be replaced by IList or similar but you cannot use IEnumarable as a guard for protecting collections/objects from modifications.</p><p>Cheers.</p> ]]></content:encoded> </item> <item><title>By: Steve</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22840</link> <dc:creator>Steve</dc:creator> <pubDate>Wed, 28 Oct 2009 11:36:29 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22840</guid> <description>@Dmitriy - That&#039;s not entirely true, the Count() extension method will first check if it can cast to IList and then access the Count property, otherwise yes it will enumerate to get the count.</description> <content:encoded><![CDATA[<p>@Dmitriy &#8211; That&#8217;s not entirely true, the Count() extension method will first check if it can cast to IList and then access the Count property, otherwise yes it will enumerate to get the count.</p> ]]></content:encoded> </item> <item><title>By: Sean Kearon</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22839</link> <dc:creator>Sean Kearon</dc:creator> <pubDate>Wed, 28 Oct 2009 11:02:22 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22839</guid> <description>I strongly disagree also.  I consider AddXXX/RemoveXXX methods noisy and very much prefer direct interaction with the collections.  I find it a more natural approach.  As Andrey mentioned, it also tends toward bloating the API.My collections have changing and changed events (can be cancelled in the case of changing), business logic is handled through configurations that use these events.</description> <content:encoded><![CDATA[<p>I strongly disagree also.  I consider AddXXX/RemoveXXX methods noisy and very much prefer direct interaction with the collections.  I find it a more natural approach.  As Andrey mentioned, it also tends toward bloating the API.</p><p>My collections have changing and changed events (can be cancelled in the case of changing), business logic is handled through configurations that use these events.</p> ]]></content:encoded> </item> <item><title>By: Awkward Coder</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22838</link> <dc:creator>Awkward Coder</dc:creator> <pubDate>Wed, 28 Oct 2009 09:25:00 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22838</guid> <description>@Davy - obivious now you&#039;ve pointed it out ;)As for the entities in the collection being mutable, I was thinking of having using a &#039;ReadOnly&#039; version of an entity that uses aggregation of the entity to expose the state but not any behaviour. These would then be returned as the contents of the collection.e.g.public class ReadOnlyOrder
{
private Order _order;public ReadOnlyOrder(Order order)
{
_order = order;
}public int Id
{
get { return _order.Id; }
}
}</description> <content:encoded><![CDATA[<p>@Davy &#8211; obivious now you&#8217;ve pointed it out <img
src='http://d18sni7re4ly7f.cloudfront.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>As for the entities in the collection being mutable, I was thinking of having using a &#8216;ReadOnly&#8217; version of an entity that uses aggregation of the entity to expose the state but not any behaviour. These would then be returned as the contents of the collection.</p><p>e.g.</p><p>public class ReadOnlyOrder<br
/> {<br
/> private Order _order;</p><p> public ReadOnlyOrder(Order order)<br
/> {<br
/> _order = order;<br
/> }</p><p> public int Id<br
/> {<br
/> get { return _order.Id; }<br
/> }<br
/> }</p> ]]></content:encoded> </item> <item><title>By: Dmitriy Nagirnyak</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22837</link> <dc:creator>Dmitriy Nagirnyak</dc:creator> <pubDate>Wed, 28 Oct 2009 09:23:42 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22837</guid> <description>You have valid points and concerns. But I do not think you are 100% correct on these:&gt; &quot;Anyone can effectively add and remove elements from the collection, or even clear them entirely, without your object knowing about it.&quot;
&gt; &quot;I can eliminate the possibility of side-effects in my code due to unexpected externally caused mutations in the state of the collection.&quot;
True. Using IEnumerable you prevent *collection changes*, but there is no way you can prevent *modification of objects themselves*:customer.ReadOnlyListOfBills.First().Amount = 0; // Still customer is not aware of the changeTo prevent this, all the objects must be linked between themselves and subscribed for any possible modifications. This leads to the whole nes set of issues.So generally speaking you can minimise possible side-effects, but you cannot totally eliminate them.And yes, the wired thing about exposing IEnumerable is that we have to use Count() extension method to obtain number of elements which is ineffective (as it iterates the collection).What would you do in this case? Expose a new property:customer.CountOfBills // returns Count from internal collectionCheers,
Dmitriy.</description> <content:encoded><![CDATA[<p>You have valid points and concerns. But I do not think you are 100% correct on these:</p><p>&gt; &#8220;Anyone can effectively add and remove elements from the collection, or even clear them entirely, without your object knowing about it.&#8221;<br
/> &gt; &#8220;I can eliminate the possibility of side-effects in my code due to unexpected externally caused mutations in the state of the collection.&#8221;<br
/> True. Using IEnumerable you prevent *collection changes*, but there is no way you can prevent *modification of objects themselves*:</p><p>customer.ReadOnlyListOfBills.First().Amount = 0; // Still customer is not aware of the change</p><p>To prevent this, all the objects must be linked between themselves and subscribed for any possible modifications. This leads to the whole nes set of issues.</p><p>So generally speaking you can minimise possible side-effects, but you cannot totally eliminate them.</p><p>And yes, the wired thing about exposing IEnumerable is that we have to use Count() extension method to obtain number of elements which is ineffective (as it iterates the collection).</p><p>What would you do in this case? Expose a new property:</p><p>customer.CountOfBills // returns Count from internal collection</p><p>Cheers,<br
/> Dmitriy.</p> ]]></content:encoded> </item> <item><title>By: Reflective Perspective - Chris Alcock &#187; The Morning Brew #464</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22836</link> <dc:creator>Reflective Perspective - Chris Alcock &#187; The Morning Brew #464</dc:creator> <pubDate>Wed, 28 Oct 2009 08:37:49 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22836</guid> <description>[...] Stop Exposing Collections Already! - Davy Brion talks about the bad practice of exposing collections to all and sundry, and by doing so allow them to manipulate and change the collection. Davy then explains a number of techniques that let you expose information from collections safely without fear of someone changing things without your knowledge [...]</description> <content:encoded><![CDATA[<p>[...] Stop Exposing Collections Already! &#8211; Davy Brion talks about the bad practice of exposing collections to all and sundry, and by doing so allow them to manipulate and change the collection. Davy then explains a number of techniques that let you expose information from collections safely without fear of someone changing things without your knowledge [...]</p> ]]></content:encoded> </item> <item><title>By: Andrey Shchekin</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22835</link> <dc:creator>Andrey Shchekin</dc:creator> <pubDate>Wed, 28 Oct 2009 08:08:12 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22835</guid> <description>I strongly disagree on AddXXX/RemoveXXX methods. Should I re-implement my AddRange/RemoveRange/RemoveWhere methods for all objects just because original developer haven&#039;t thought of these scenarios? What about Clear?I think providing rich API and managing side-effects (which is not hard, using a event-sending or custom collection if really needed) is a sign of better design than limited modification points. Just because in second case it is much easier to forget some useful method and much harder to work with this API in a generic way.</description> <content:encoded><![CDATA[<p>I strongly disagree on AddXXX/RemoveXXX methods. Should I re-implement my AddRange/RemoveRange/RemoveWhere methods for all objects just because original developer haven&#8217;t thought of these scenarios? What about Clear?</p><p>I think providing rich API and managing side-effects (which is not hard, using a event-sending or custom collection if really needed) is a sign of better design than limited modification points. Just because in second case it is much easier to forget some useful method and much harder to work with this API in a generic way.</p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22834</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Wed, 28 Oct 2009 07:53:32 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22834</guid> <description>a ReadOnlyCollection is basically a collection that only allows enumeration or access by index... both are things that are possible with IEnumerable and you avoid being tied to a specific implementation</description> <content:encoded><![CDATA[<p>a ReadOnlyCollection is basically a collection that only allows enumeration or access by index&#8230; both are things that are possible with IEnumerable and you avoid being tied to a specific implementation</p> ]]></content:encoded> </item> <item><title>By: Awkward Coder</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22833</link> <dc:creator>Awkward Coder</dc:creator> <pubDate>Wed, 28 Oct 2009 07:50:09 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22833</guid> <description>Total agree all exposed collections should be immutable, why not expose ReadOnlyCollection? not ideal as it&#039;s not an interface but it is explicit in the intention.</description> <content:encoded><![CDATA[<p>Total agree all exposed collections should be immutable, why not expose ReadOnlyCollection? not ideal as it&#8217;s not an interface but it is explicit in the intention.</p> ]]></content:encoded> </item> <item><title>By: Gabriel Lozano-Moran</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22832</link> <dc:creator>Gabriel Lozano-Moran</dc:creator> <pubDate>Wed, 28 Oct 2009 07:07:36 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22832</guid> <description>Basically this only becomes a problem when you are creating a public interface that will not only be consumed by your code. Then again exposing a collection as IEnumerable or even as an Array as Ken mentions, it only provides you shallow immutability. When returning an array, you could decide to return a Clone() but the again Clone() = shallow copy.Better would be to not expose a collection at all. If you need to do see then it is time to reconsider the design.</description> <content:encoded><![CDATA[<p>Basically this only becomes a problem when you are creating a public interface that will not only be consumed by your code. Then again exposing a collection as IEnumerable or even as an Array as Ken mentions, it only provides you shallow immutability. When returning an array, you could decide to return a Clone() but the again Clone() = shallow copy.</p><p>Better would be to not expose a collection at all. If you need to do see then it is time to reconsider the design.</p> ]]></content:encoded> </item> <item><title>By: Ken Egozi</title><link>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/comment-page-1/#comment-22831</link> <dc:creator>Ken Egozi</dc:creator> <pubDate>Wed, 28 Oct 2009 06:33:18 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/?p=1724#comment-22831</guid> <description>I second everything, however I&#039;d replace &quot;IEnumerable&quot; with &quot;T[]&quot;.
IEnumerable implies &quot;infinite&quot; and &quot;lazy-evaluated&quot;, and many times this is not the case.
it&#039;s good within contexts of building linq (or other) queries, that benefit directly from the lazy loading. once the data is loaded in-memory, an Array is the simplest representation of the data.</description> <content:encoded><![CDATA[<p>I second everything, however I&#8217;d replace &#8220;IEnumerable&#8221; with &#8220;T[]&#8220;.<br
/> IEnumerable implies &#8220;infinite&#8221; and &#8220;lazy-evaluated&#8221;, and many times this is not the case.<br
/> it&#8217;s good within contexts of building linq (or other) queries, that benefit directly from the lazy loading. once the data is loaded in-memory, an Array is the simplest representation of the data.</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/24 queries in 0.012 seconds using disk: basic
Object Caching 648/649 objects using disk: basic
Content Delivery Network via Amazon Web Services: CloudFront: d18sni7re4ly7f.cloudfront.net

Served from: davybrion.com @ 2012-05-23 00:58:39 -->
