I have moved my active blog over to tumblr. I've maintained this blog for reference but will be posting to http://www.robustsoftware.co.uk instead. I've pointed my Feedburner feed to tumblr so if you're subscribed already you should already have switched with me.

My version of a SmartBag

After seeing Jeffrey Palermo's SmartBag (part 1 and part 2) I was inspired to create my own version which was more specific to my needs. For one I'm a lover of the IEnumerable<T> interface, especially when combined with the power of LINQ for Objects.

Anyway, enough of my waffle, here's the code for the class.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5.  
  6. namespace SmartBag
  7. {
  8.     public class SmartBag
  9.     {
  10.         private Hashtable _hashtable;
  11.  
  12.         public SmartBag()
  13.         {
  14.             this._hashtable = new Hashtable();
  15.         }
  16.  
  17.         public void Add<T>(T item)
  18.         {
  19.             List<T> list;
  20.  
  21.  
  22.             if (!this._hashtable.ContainsKey(typeof(T)))
  23.                 this._hashtable.Add(typeof(T), new List<T>());
  24.  
  25.             list = this._hashtable[typeof(T)] as List<T>;
  26.             list.Add(item);
  27.         }
  28.  
  29.         public IEnumerable<T> Get<T>()
  30.         {
  31.             if (!this._hashtable.ContainsKey(typeof(T)))
  32.                 throw new IndexOutOfRangeException(string.Format("There are no entries for {0}", typeof(T).Name));
  33.  
  34.             foreach (T item in this._hashtable[typeof(T)] as List<T>)
  35.                 yield return item;
  36.         }
  37.     }
  38. }

There are basically two methods to it, Add and Get. Add adds items of the specified type to a list of that type. Get retrieves the list of the specified type.

Below are my test cases which should give you some ideas of how you can use it.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MbUnit.Framework;
  6.  
  7. namespace SmartBag
  8. {
  9.     [TestFixture]
  10.     class SmartBagTest
  11.     {
  12.         [Test]
  13.         public void AClassAddedAgainstAnInterfaceCanBeRetrievedTogether()
  14.         {
  15.             SmartBag bag = new SmartBag();
  16.  
  17.  
  18.             bag.Add<IContract>(new Parent());
  19.             bag.Add<IContract>(new Child());
  20.  
  21.             Assert.AreEqual(2, bag.Get<IContract>().Count());
  22.         }
  23.  
  24.         [Test]
  25.         public void AChildWillBeAddedToItsParentsEnumerationIfYouDefineIt()
  26.         {
  27.             SmartBag bag = new SmartBag();
  28.  
  29.  
  30.             bag.Add(new Parent());
  31.             bag.Add<Parent>(new Child());
  32.  
  33.             Assert.AreEqual(2, bag.Get<Parent>().Count());
  34.         }
  35.  
  36.         [Test, ExpectedException(typeof(IndexOutOfRangeException))]
  37.         public void AnExpectionWillBeRaiseWhenAttemptingToRetrieveATypeThatDoesNotExist()
  38.         {
  39.             SmartBag bag = new SmartBag();
  40.  
  41.  
  42.             foreach (Object item in bag.Get<Object>()) { } // must enumerate for it to be evaluated
  43.         }
  44.  
  45.         [Test]
  46.         public void ShouldBeAbleToAddChildAsADifferentType()
  47.         {
  48.             SmartBag bag = new SmartBag();
  49.  
  50.  
  51.             bag.Add(new Parent());
  52.             bag.Add(new Child());
  53.  
  54.             Assert.AreEqual(1, bag.Get<Parent>().Count());
  55.             Assert.AreEqual(1, bag.Get<Child>().Count());
  56.         }
  57.  
  58.         private class Parent : IContract
  59.         {
  60.         }
  61.  
  62.         private class Child : Parent
  63.         {
  64.         }
  65.  
  66.         private interface IContract
  67.         {
  68.         }
  69.     }
  70. }

To begin with this is a basic idea. I hope to utilise it in an upcoming project and it will probably look a bit different after I've used it in anger. Something I can see I might need is to extract an interface to make testing easier but I've left that out for now as YAGNI may apply.

Any feedback would be greatly appreciated.