Avoid Exposing Collections Directly as Properties
page 1 of 6
Published: 22 Feb 2011
Unedited - Community Contributed
.NET makes it easy to create strongly typed collections and expose them as properties of our classes. However, this generally results in a design that fails at encapsulation and exposes too much of the class's internal state. Learn a few simple techniques to keep from going down this path in this article.
by Steven Smith
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28582/ 32


Sometimes your domain objects have one-to-many or many-to-many relationships with other objects.  For instance, your Customers might have collections of Orders associated with them.  The simplest way to model this is to expose a List<Order> field or property off of your Customer class.  Unfortunately, this has a number of negative consequences because of how it breaks encapsulation.  Fortunately, there are some effective ways to shield yourself from these issues while still exposing the collection's data to your class's clients.

View Entire Article

User Comments

Title: Picture links all broken (404)   
Name: Anonymous
Date: 2012-12-12 8:46:55 AM
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

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 ?


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

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-06-22 9:38:41 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search