Click here to Skip to main content
Click here to Skip to main content

Collection accessors language feature

By , 6 Dec 2012
 

Collections are independent types from the domain objects on which they are used to define properties, yet they are usually closely tied to internal business logic of the single domain object that "owns" them.

What do you do in each of the following scenarios:

  1. You have business logic that determines whether or not an item can be added to the collection.
  2. You have some logic that needs to run before an item is added to the collection and another piece of logic that need run after the item was added.

These are the possible approaches that I can think of for tackling these scenarios:

  1. Put that logic in the view-model. The logic will need to be repeated everywhere the collection is modified.
  2. Expose the collection as IEnumerable then add Add/Remove methods directly onto the domain object where you put the check logic. This works but the public collection property can always be cast to ICollection or derivative and directly modified, thus violating the intended encapsulation.
  3. Create a custom collection.

But I do not feel satisfied with any of these options. I always wished to see a language feature like the add/remove block that was introduced in C# for events but as being available for public collection properties as well.

public collection IList<orderdetails> Details 
{
   add { if (isMaxReached) throw new Exception("max reached"); details.Add(value); }
   remove { details.Remove(value); } 
}

All modifications to the collection would pass through these accessors.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Adrian Alexander
United States United States
Member
Adrian loves facilitating suave user experiences via the latest and greatest GUI technologies such as Windows 8 Metro-style apps as well as WPF. More generally, he finds joy in architecting software that is easy to comprehend and maintain. He does so by applying design patterns at the top-level, and by incessantly refactoring code at lower levels. He's always interested in hearing about opportunities for full or part-time development work. He resides in Pennsylvania but can potentially travel anywhere in the country. (Writing about himself in the third-person is Adrian's new hobby.)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 6 Dec 2012
Article Copyright 2012 by Adrian Alexander
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid