1: /// <summary>
2: /// Maps a key to a list of data
3: /// </summary>
4: /// <typeparam name="T1">Key value</typeparam>
5: /// <typeparam name="T2">Type that the list should contain</typeparam>
6: public class ListMapping<T1,T2>
7: {
8: #region Constructors
9:
10: /// <summary>
11: /// Constructor
12: /// </summary>
13: public ListMapping()
14: {
15: }
16:
17: #endregion
18:
19: #region Private Variables
20:
21: protected Dictionary<T1, List<T2>> Items = new Dictionary<T1, List<T2>>();
22:
23: #endregion
24:
25: #region Public Functions
26:
27: /// <summary>
28: /// Adds an item to the mapping
29: /// </summary>
30: /// <param name="Key">Key value</param>
31: /// <param name="Value">The value to add</param>
32: public virtual void Add(T1 Key, T2 Value)
33: {
34: try
35: {
36: if (Items.ContainsKey(Key))
37: {
38: Items[Key].Add(Value);
39: }
40: else
41: {
42: Items.Add(Key, new List<T2>());
43: Items[Key].Add(Value);
44: }
45: }
46: catch { throw; }
47: }
48:
49: /// <summary>
50: /// Determines if a key exists
51: /// </summary>
52: /// <param name="key">Key to check on</param>
53: /// <returns>True if it exists, false otherwise</returns>
54: public virtual bool ContainsKey(T1 key)
55: {
56: try
57: {
58: return Items.ContainsKey(key);
59: }
60: catch { throw; }
61: }
62:
63: /// <summary>
64: /// The list of keys within the mapping
65: /// </summary>
66: public virtual ICollection<T1> Keys
67: {
68: get { try { return Items.Keys; } catch { throw; } }
69: }
70:
71: /// <summary>
72: /// Remove a list of items associated with a key
73: /// </summary>
74: /// <param name="key">Key to use</param>
75: /// <returns>True if the key is found, false otherwise</returns>
76: public virtual bool Remove(T1 key)
77: {
78: try
79: {
80: return Items.Remove(key);
81: }
82: catch { throw; }
83: }
84:
85: /// <summary>
86: /// Gets a list of values associated with a key
87: /// </summary>
88: /// <param name="key">Key to look for</param>
89: /// <returns>The list of values</returns>
90: public virtual List<T2> this[T1 key]
91: {
92: get
93: {
94: return Items[key];
95: }
96: set
97: {
98: Items[key] = value;
99: }
100: }
101:
102: /// <summary>
103: /// Clears all items from the listing
104: /// </summary>
105: public virtual void Clear()
106: {
107: Items.Clear();
108: }
109:
110: /// <summary>
111: /// The number of items in the listing
112: /// </summary>
113: public virtual int Count
114: {
115: get { return Items.Count; }
116: }
117:
118:
119: #endregion
120: }