Comments for Claudio Bernasconi's TechBloghttp://www.claudiobernasconi.ch
Not only writes code, but solves problems.Sun, 10 Sep 2017 11:45:00 +0000hourly1https://wordpress.org/?v=4.8.2Comment on When To Use IEnumerable, ICollection, IList And List by Frédéric Delaportehttp://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/#comment-2251
Sun, 10 Sep 2017 11:45:00 +0000http://www.claudiobernasconi.ch/?p=442#comment-2251> Returning ICollection or IList should be avoided because they are inviting clients to mutate them.

And then? Where is the harm? Why should the consumer not mutate them if it needs too? Of course this requires those collections to not be bound to some internal state of the library if it is not meant to be impacted by consumer actions on them.

About IEnumerable, in “old times” deferred execution enumerables were not that common, so developers were not much caring avoiding iterating them many times. And some still do not care nowadays, causing unneeded multiple queries on db when its underlying implementation is a queryable by example. For this reason I usually return query results as “fire&forget” IList. The consumer is free to mutate them as it wants, that will not have any consequences on the layer having emitted them.

Exposing a mutable collection is an error only when it is bound to some state that is not supposed to be changed directly by the consumer.

]]>Comment on Microsoft 70-483 Exam Preparation Guide by Siddharthhttp://www.claudiobernasconi.ch/2013/01/15/microsoft-70-483-exam-preparation-guide/#comment-2250
Sat, 26 Aug 2017 10:28:00 +0000http://www.claudiobernasconi.ch/?p=218#comment-2250Thanks for your efforts. Much appreciated !
]]>Comment on How To Fix Common Errors Using ASP.NET MVC Scaffolding by Mubarak Abdallahttp://www.claudiobernasconi.ch/2015/06/22/how-to-fix-common-errors-using-asp-net-mvc-scaffolding/#comment-2249
Thu, 24 Aug 2017 19:16:00 +0000http://www.claudiobernasconi.ch/?p=1150#comment-2249In my case, I thought that the problem is due to an update I made in the myappModel.edmx diagram. (I removed an entity that I have added by mistake, and re-added the correct one.) maybe that was the reason that I had to re-generate the Edit view using scaffolding.
]]>Comment on How To Fix Common Errors Using ASP.NET MVC Scaffolding by Mubarak Abdallahttp://www.claudiobernasconi.ch/2015/06/22/how-to-fix-common-errors-using-asp-net-mvc-scaffolding/#comment-2248
Thu, 24 Aug 2017 19:14:00 +0000http://www.claudiobernasconi.ch/?p=1150#comment-2248Hi Claudio
Thanks for the post, in my case I was able to fix the problem of exception generated when trying to save a new entry, or save an edit to a record (in my application).

– I have to delete and re-create the Edit View. And the save edit feature worked in my application.

– As for the create new entry, I also re-create the Create View, but had to remove the text box that request the primary key id, the database auto generate the primary key, so when I send another id the application breaks.

]]>Comment on When To Use IEnumerable, ICollection, IList And List by Lasse Schouhttp://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/#comment-2247
Wed, 23 Aug 2017 12:28:00 +0000http://www.claudiobernasconi.ch/?p=442#comment-2247I know this article was written some years ago, but the discussion is still highly relevant. In my experience, the real choice should be between IEnumerable and IReadOnlyCollection because both return immutable types and thereby “safe”. Returning ICollection or IList should be avoided because they are inviting clients to mutate them.

What I’ve found is that an interface method that returns IEnumerable can be dangerous because the result is often a stream of an expensive resource (database, file, etc). Some developers would feel the need to call .ToList() to make sure the list is “closed”, and iterating through the result twice wouldn’t feel safe (resharper also warns against this). Also, calling .Count() on the result could turn out very expensive, but the caller would have no real knowledge about this, if just looking at the interface.

In short, IEnumerable should be seen as a “stream”.

IReadOnlyColleciton on the other hand is a closed list which adds the .Count property. It’s safe to enumerate multiple and getting the count is cheap and fast.

So in conclusion, I’ve found that an interface should return IEnumerable to signal that the resource is yielded out as a stream, potentially not cheap/fast. And IReadOnlyCollection should be used for “closed” lists.

Would love to hear what others think.

]]>Comment on When To Use IEnumerable, ICollection, IList And List by Ananth Kmhttp://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/#comment-2246
Sat, 29 Jul 2017 00:09:00 +0000http://www.claudiobernasconi.ch/?p=442#comment-2246yes. Thanks so much Claudio
]]>Comment on When To Use IEnumerable, ICollection, IList And List by Claudio Bernasconihttp://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/#comment-2245
Fri, 28 Jul 2017 10:44:00 +0000http://www.claudiobernasconi.ch/?p=442#comment-2245Hi, exposing internal data structures to public apis is never a good idea. If you want to change the internal behaviour of your code this could lead into a breaking change. Furthermore, you should let your consumer operate on your data using methods specifically designed for a purpose e.g. adding and item or removing it. If you expose the entire list to a public api you loose the control over it. The consumer can do whatever he wants to do. This could result in a bug or unintended behavior of your code. I hope this explains what you were asking for.
]]>Comment on When To Use IEnumerable, ICollection, IList And List by Rubenhttp://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/#comment-2244
Tue, 25 Jul 2017 14:41:00 +0000http://www.claudiobernasconi.ch/?p=442#comment-2244What do you mean exactly with “you should never have a member of your own implementations with the concrete type List/List”. Could you explain it in more detail or with an example? Thanx. Great article btw
]]>Comment on When To Use IEnumerable, ICollection, IList And List by Ananth Kmhttp://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/#comment-2242
Tue, 25 Jul 2017 00:37:00 +0000http://www.claudiobernasconi.ch/?p=442#comment-2242Hi Claudio
Thank you so much for the article.
You say – “Don’t expose a List as a property to public consumers. This would end in a unconfortable situation soon.”

Could you please explain.. Thank you in advance.

]]>Comment on When To Use IEnumerable, ICollection, IList And List by SandeepReddy Chhttp://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/#comment-2241
Fri, 07 Jul 2017 23:27:00 +0000http://www.claudiobernasconi.ch/?p=442#comment-2241Thanks you for detailed explanation. Though i did not write blogs , i got inspired from this article to write a at least one blog like yours. i liked your explanation and In fact it helped me to understand the science behind the ICollection and IList.
]]>