Avoid Exposing Collections Directly as Properties
page 5 of 6
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28614/ 69

Wrapping Collections

The built-in .NET class, ReadOnlyCollection<T> is the standard way to wrap a collection and make available only a read-only version of the collection's contents.  The collection lives in the System.Collections.ObjectModel namespace.  Once we update our Order class to expose this collection as the type of its OrderHistory property, our SneakyOrderHistoryCannotBeCleared() test no longer compiles, since it's not possible to cast a ReadOnlyCollection<T> to a List<T>.  The final version of the Customer class looks like this:

 


View Entire Article

User Comments

Title: Picture links all broken (404)   
Name: Anonymous
Date: 2012-12-12 8:46:55 AM
Comment:
The images are blank and result in a 404 if opened individually.
Title: Very nice   
Name: Thanigainathan
Date: 2011-03-28 2:36:03 PM
Comment:
Hi,

The article is very nice. So you mean only the parent of the list property can modify its state. Will this be sort of restricting the features of collections ?

Thanks,
Thani






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 7:37:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search