Other Posts in Data Types

  1. Replacing List
  2. Factory Pattern using Generics in C#
  3. Vector Class and Events in .Net
  4. Binary Trees in C# using Generics
  5. Priority Queue in C#
  6. Cheap Trick for Fluent Interfaces
  7. Building an DI Container

Replacing List

8/5/2008

I have to say that the set of container classes that come with .Net are fantastic in my eyes (then again, I came from the world of PHP, C, etc. so I guess something is better than nothing). For example the List class usually gets a lot of use for good reason. The ability to compare, sort, add, and remove items, not to mention the foreach capabilities, make it a fantastic generic piece of code. However, there are a couple of downsides to it.

As an example of one of the issues, let's look at a simple scenario. Let's say you have a website that has two business objects: Blog and Post. The Blog object has some basic information (name of the blog, etc.) and the Post object holds the individual posts for the blog. As such the Blog object has a list containing the various Post objects. Let's also say that for some odd reason, you don't want the Post object to be able to save itself. The only way to save the information is through the Blog object. So the question becomes, how does the Blog object know that we've added a new Post to the list?

   1: Blog.PostList.Add(Post);
   2: Blog.Save();

At this stage, how does it know what to save? In fact, how does the Blog object know if anything has even changed? Basically we can keep a second list telling us what the Blog object looked like prior to saving (amazingly enough, I see this approach quite often), we can use the ITrackChanges interface but we still don't know when a change occurs as we have to set that ourselves, we can make our own List object, etc. Most of the options here are poor at best, with the exception of one... Rewrite List... Not really, more just inherit from List.

We can easily tell when that new post is added by simply setting up an event and having the Blog object use that event to get the information it needs. However, List doesn't have any events associated with it. There is no way, without some modification, that we can do this. So let's do some modification:

   1: /*
   2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
   3: 
   4: Permission is hereby granted, free of charge, to any person obtaining a copy
   5: of this software and associated documentation files (the "Software"), to deal
   6: in the Software without restriction, including without limitation the rights
   7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8: copies of the Software, and to permit persons to whom the Software is
   9: furnished to do so, subject to the following conditions:
  10: 
  11: The above copyright notice and this permission notice shall be included in
  12: all copies or substantial portions of the Software.
  13: 
  14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20: THE SOFTWARE.*/
  21:  
  22: #region Usings
  23: using System;
  24: using Utilities.Events;
  25: using Utilities.Events.EventArgs;
  26: #endregion
  27:  
  28: namespace Utilities.DataTypes
  29: {
  30:     /// <summary>
  31:     /// Class designed to replace List. Contains events so that we can tell
  32:     /// when the list has been changed.
  33:     /// </summary>
  34:     public class List<T> : System.Collections.Generic.List<T>
  35:     {
  36:         #region Events
  37:         public EventHandler<ChangedEventArgs> Changed;
  38:         #endregion
  39:  
  40:         #region Public Functions
  41:  
  42:         public new void Add(T value)
  43:         {
  44:             base.Add(value);
  45:             ChangedEventArgs TempArgs = new ChangedEventArgs();
  46:             TempArgs.Content = PropertyName;
  47:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
  48:         }
  49:  
  50:         public new void AddRange(System.Collections.Generic.IEnumerable<T> value)
  51:         {
  52:             base.AddRange(value);
  53:             ChangedEventArgs TempArgs = new ChangedEventArgs();
  54:             TempArgs.Content = PropertyName;
  55:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
  56:         }
  57:  
  58:         public new bool Remove(T obj)
  59:         {
  60:             bool ReturnValue = base.Remove(obj);
  61:             ChangedEventArgs TempArgs = new ChangedEventArgs();
  62:             TempArgs.Content = PropertyName;
  63:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
  64:             return ReturnValue;
  65:         }
  66:  
  67:         public new void RemoveAt(int index)
  68:         {
  69:             base.RemoveAt(index);
  70:             ChangedEventArgs TempArgs = new ChangedEventArgs();
  71:             TempArgs.Content = PropertyName;
  72:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
  73:         }
  74:  
  75:         public new int RemoveAll(Predicate<T> match)
  76:         {
  77:             int ReturnValue = base.RemoveAll(match);
  78:             ChangedEventArgs TempArgs = new ChangedEventArgs();
  79:             TempArgs.Content = PropertyName;
  80:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
  81:             return ReturnValue;
  82:         }
  83:  
  84:         public new void RemoveRange(int index, int count)
  85:         {
  86:             base.RemoveRange(index, count);
  87:             ChangedEventArgs TempArgs = new ChangedEventArgs();
  88:             TempArgs.Content = PropertyName;
  89:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
  90:         }
  91:  
  92:         public new void Insert(int index, T value)
  93:         {
  94:             base.Insert(index, value);
  95:             ChangedEventArgs TempArgs = new ChangedEventArgs();
  96:             TempArgs.Content = PropertyName;
  97:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
  98:         }
  99:  
 100:         public new void InsertRange(int index, System.Collections.Generic.IEnumerable<T> collection)
 101:         {
 102:             base.InsertRange(index, collection);
 103:             ChangedEventArgs TempArgs = new ChangedEventArgs();
 104:             TempArgs.Content = PropertyName;
 105:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
 106:         }
 107:  
 108:         public new void Clear()
 109:         {
 110:             base.Clear();
 111:             ChangedEventArgs TempArgs = new ChangedEventArgs();
 112:             TempArgs.Content = PropertyName;
 113:             EventHelper.Raise<ChangedEventArgs>(Changed, this, TempArgs);
 114:         }
 115:  
 116:         #endregion
 117:  
 118:         #region Properties
 119:  
 120:         public new T this[int index]
 121:         {
 122:             get { return base[index]; }
 123:             set
 124:             {
 125:                 base[index] = value;
 126:                 EventHelper.Raise<ChangedEventArgs>(Changed, this, new ChangedEventArgs());
 127:             }
 128:         }
 129:  
 130:         public string PropertyName { get; set; }
 131:  
 132:         #endregion
 133:     }
 134: }

The code above simply inherits from List and hides the various functions that can be used to modify the list (Add, Remove, etc.). It calls the base object on each of these calls, thus we don't have to rewrite the code, and at the same time it calls an event. The Blog object can easily tie into this, have a function called, and know that changes have taken place. Thus when an item is added to the list, we know when it happens, and can find out what has been added so we know what needs to be saved. In turn, we're saved from the, potentially, numerous calls to our database... Anyway, hopefully you'll find the code useful. So try it out, leave feedback, and happy coding.



Comments