Created
August 14, 2010 23:23
-
-
Save jsmale/524832 to your computer and use it in GitHub Desktop.
Dictionary Adapter for HttpSessionState
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CollectionAdapter<T> : ICollection<T>, ICollection | |
{ | |
readonly ICollection collection; | |
public void CopyTo(Array array, int index) | |
{ | |
collection.CopyTo(array, index); | |
} | |
public object SyncRoot | |
{ | |
get { return collection.SyncRoot; } | |
} | |
public bool IsSynchronized | |
{ | |
get { return collection.IsSynchronized; } | |
} | |
public CollectionAdapter(ICollection collection) | |
{ | |
this.collection = collection; | |
} | |
IEnumerator<T> IEnumerable<T>.GetEnumerator() | |
{ | |
return collection.Cast<T>().GetEnumerator(); | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
return collection.GetEnumerator(); | |
} | |
public void Add(T item) | |
{ | |
throw new NotSupportedException(); | |
} | |
public void Clear() | |
{ | |
throw new NotSupportedException(); | |
} | |
public bool Contains(T item) | |
{ | |
return collection.Cast<T>().Any(x => Equals(x, item)); | |
} | |
public void CopyTo(T[] array, int arrayIndex) | |
{ | |
collection.CopyTo(array, arrayIndex); | |
} | |
public bool Remove(T item) | |
{ | |
throw new NotSupportedException(); | |
} | |
public int Count | |
{ | |
get { return collection.Count; } | |
} | |
public bool IsReadOnly | |
{ | |
get { return true; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DictionaryEnumerator<TKey,TValue> : IDictionaryEnumerator, IDisposable | |
{ | |
readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator; | |
public DictionaryEnumerator(IEnumerator<KeyValuePair<TKey,TValue>> enumerator) | |
{ | |
this.enumerator = enumerator; | |
} | |
public void Dispose() | |
{ | |
enumerator.Dispose(); | |
} | |
public bool MoveNext() | |
{ | |
return enumerator.MoveNext(); | |
} | |
public void Reset() | |
{ | |
enumerator.Reset(); | |
} | |
public object Current | |
{ | |
get { return enumerator.Current; } | |
} | |
public object Key | |
{ | |
get { return enumerator.Current.Key; } | |
} | |
public object Value | |
{ | |
get { return enumerator.Current.Value; } | |
} | |
public DictionaryEntry Entry | |
{ | |
get { return new DictionaryEntry(Key, Value); } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IWebSession | |
{ | |
SearchRequest LastSearchRequest { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SearchController | |
{ | |
readonly ISearchService searchService; | |
readonly IWebSession webSession; | |
public SearchController(ISearchService searchService, IWebSession webSession) | |
{ | |
this.searchService = searchService; | |
this.webSession = webSession; | |
} | |
public SearchResponse SearchResults(SearchInput searchInput) | |
{ | |
var searchRequest = webSession.SupplierSearchRequest | |
?? new SearchRequest(); | |
return searchService(searchRequest, searchInput.Page); | |
} | |
public FubuContinuation SearchSuppliers(SupplierSearchRequest supplierSearchRequest) | |
{ | |
webSession.SupplierSearchRequest = supplierSearchRequest; | |
return FubuContinuation.RedirectTo(new SearchInput{Page = 1}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SessionDictionary : IDictionary<string,object>, IDictionary | |
{ | |
readonly HttpSessionState sessionState; | |
readonly CollectionAdapter<string> keysAdapter; | |
readonly CollectionAdapter<object> valuesAdapter; | |
public SessionDictionary(HttpSessionState sessionState) | |
{ | |
this.sessionState = sessionState; | |
keysAdapter = new CollectionAdapter<string>(sessionState.Keys); | |
valuesAdapter = new CollectionAdapter<object>(sessionState); | |
} | |
public bool ContainsKey(string key) | |
{ | |
return keysAdapter.Contains(key); | |
} | |
public void Add(string name, object value) | |
{ | |
sessionState.Add(name, value); | |
} | |
public bool Remove(string name) | |
{ | |
if (!ContainsKey(name)) return false; | |
sessionState.Remove(name); | |
return true; | |
} | |
public bool TryGetValue(string key, out object value) | |
{ | |
if (ContainsKey(key)) | |
{ | |
value = this[key]; | |
return true; | |
} | |
value = null; | |
return false; | |
} | |
public void Add(KeyValuePair<string, object> item) | |
{ | |
Add(item.Key, item.Value); | |
} | |
public bool Contains(object key) | |
{ | |
return ContainsKey((string) key); | |
} | |
public void Add(object key, object value) | |
{ | |
Add((string)key, value); | |
} | |
public void Clear() | |
{ | |
sessionState.Clear(); | |
} | |
IDictionaryEnumerator IDictionary.GetEnumerator() | |
{ | |
return new DictionaryEnumerator<string, object>(KeyValueEnumerable().GetEnumerator()); | |
} | |
public void Remove(object key) | |
{ | |
Remove((string) key); | |
} | |
object IDictionary.this[object key] | |
{ | |
get { return this[(string) key]; } | |
set { this[(string) key] = value; } | |
} | |
public bool Contains(KeyValuePair<string, object> item) | |
{ | |
if (!ContainsKey(item.Key)) return false; | |
return Equals(this[item.Key], item.Value); | |
} | |
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) | |
{ | |
CopyTo((Array)array, arrayIndex); | |
} | |
public bool Remove(KeyValuePair<string, object> item) | |
{ | |
if (!Contains(item)) return false; | |
return Remove(item.Key); | |
} | |
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() | |
{ | |
return KeyValueEnumerable().GetEnumerator(); | |
} | |
IEnumerable<KeyValuePair<string, object>> KeyValueEnumerable() | |
{ | |
return Keys.Select(key => new KeyValuePair<string, object>(key, this[key])); | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
return sessionState.GetEnumerator(); | |
} | |
public object this[string name] | |
{ | |
get { return sessionState[name]; } | |
set { sessionState[name] = value; } | |
} | |
public void CopyTo(Array array, int index) | |
{ | |
sessionState.CopyTo(array, index); | |
} | |
public int Count | |
{ | |
get { return sessionState.Count; } | |
} | |
public object SyncRoot | |
{ | |
get { return sessionState.SyncRoot; } | |
} | |
public bool IsSynchronized | |
{ | |
get { return sessionState.IsSynchronized; } | |
} | |
public ICollection<string> Keys | |
{ | |
get { return keysAdapter; } | |
} | |
ICollection IDictionary.Values | |
{ | |
get { return valuesAdapter; } | |
} | |
ICollection IDictionary.Keys | |
{ | |
get { return keysAdapter; } | |
} | |
public ICollection<object> Values | |
{ | |
get { return valuesAdapter; } | |
} | |
public bool IsReadOnly | |
{ | |
get { return sessionState.IsReadOnly; } | |
} | |
public bool IsFixedSize | |
{ | |
get { return false; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WebRegistry : Registry | |
{ | |
public WebRegistry() | |
{ | |
var dictionaryAdapterFactory = new DictionaryAdapterFactory(); | |
For<IWebSession>() | |
.HttpContextScoped() | |
.Use(() => dictionaryAdapterFactory.GetAdapter<IWebSession>(new SessionDictionary(HttpContext.Current.Session))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment