joshka left a comment on my previous post that reads...

"... Can you talk about the Application Context and IKey stuff a little in a future post?"

The IKey interface defines a contract for different keys that are put into a dictionary. It depends on the implementation of the key to know how to parse its value out of the dictionary.

1 public interface IKey<T>
2   {
3       bool is_found_in(IDictionary items);
4       T parse_from(IDictionary items);
5       void remove_from(IDictionary items);
6       void add_value_to(IDictionary items, T value);
7   }

An implementation of the key that we used for shoving an ISession into the HttpContext.Items collection is the TypedKey. It creates a unique key based on type T.

 1 internal class TypedKey<T> : IKey<T>
 2   {
 3       public bool is_found_in(IDictionary items)
 4       {
 5           return items.Contains(create_unique_key());
 6       }
 7   
 8       public T parse_from(IDictionary items)
 9       {
10           return (T) items[create_unique_key()];
11       }
12   
13       public void remove_from(IDictionary items)
14       {
15           if (is_found_in(items))
16           {
17               items.Remove(create_unique_key());
18           }
19       }
20   
21       public void add_value_to(IDictionary items, T value)
22       {
23           items[create_unique_key()] = value;
24       }
25   
26       public bool Equals(TypedKey<T> obj)
27       {
28           return !ReferenceEquals(null, obj);
29       }
30   
31       public override bool Equals(object obj)
32       {
33           if (ReferenceEquals(null, obj)) return false;
34           if (ReferenceEquals(this, obj)) return true;
35           if (obj.GetType() != typeof (TypedKey<T>)) return false;
36           return Equals((TypedKey<T>) obj);
37       }
38   
39       public override int GetHashCode()
40       {
41           return GetType().GetHashCode();
42       }
43   
44       private string create_unique_key()
45       {
46           return GetType().FullName;
47       }
48     }

It knows how to add a value in to the dictionary using it as the key, and how to parse values from the dictionary using it. The application context can be an adapter around the HttpContext, or a hand rolled context for win forms. An implementation on the web might look like....

 1 public class WebContext : IApplicationContext
 2   {
 3       public bool contains<T>(IKey<T> key)
 4       {
 5           return key.is_found_in(HttpContext.Current.Items);
 6       }
 7   
 8       public void add<T>(IKey<T> key, T value)
 9       {
10           key.add_value_to(HttpContext.Current.Items, value);
11       }
12   
13       public T get_value_for<T>(IKey<T> key)
14       {
15           return key.parse_from(HttpContext.Current.Items);
16       }
17   
18       public void remove(IKey<ISession> key)
19       {
20           key.remove_from(HttpContext.Current.Items);
21       }
22   }

When running your integration tests, you can swap out the implementation with an implementation specific to running unit tests.

comments powered by Disqus