I'm adding additional functionality to an existing MVC .net application, and to help prevent or at least reduce repeated reads to the dB I'm dumping a few custom entities in session. I'm limiting what can be stored in session to say a max of 5 objects for now.
Given below is the code and it seems generic enough and works fine locally on my box; any suggestions to improve this and session is In-Proc but if it does get stored in a persistent medium say SQL server then I would need to customize this further - decorate custom object properties w/XML element attributes
public class QuestionModelSessionStateProvider : IQuestionModelSessionStateProvider
{
const int MaxSize = 5;
public T GetValue<T>(string key)
{
HttpSessionState session = GetSessionState();
T returnValue = default(T);
if (session == null) return returnValue;
if (session["QuestionModelStore"] != null)
{
var questionModelStore = (List<KeyValuePair<string, T>>)session["QuestionModelStore"];
foreach (var t in questionModelStore.Where(t => t.Key.Equals(key)))
{
returnValue = t.Value;
}
}
return returnValue;
}
public void SetValue<T>(string key, T value)
{
HttpSessionState session = GetSessionState();
if (session == null) return;
List<KeyValuePair<string, T>> questionModelStore;
if (session["QuestionModelStore"] == null)
{
questionModelStore = new List<KeyValuePair<string, T>> { new KeyValuePair<string, T>(key, value) };
session["QuestionModelStore"] = questionModelStore;
}
else
{
questionModelStore = (List<KeyValuePair<string, T>>)session["QuestionModelStore"];
questionModelStore.Add(new KeyValuePair<string, T>(key, value));
}
if (questionModelStore.Count > MaxSize)
{
questionModelStore.RemoveAt(0);
}
}
public void RemoveValue<T>(string key)
{
HttpSessionState session = GetSessionState();
int indexToRemoveAt = 0;
var found = false;
if (session != null && session["QuestionModelStore"] != null)
{
var questionModelStore = (List<KeyValuePair<string, T>>) session["QuestionModelStore"];
for (var i = 0; i < questionModelStore.Count; i++)
{
if (questionModelStore[i].Key.Equals(key))
{
indexToRemoveAt = i;
found = true;
break;
}
}
if (found)
questionModelStore.RemoveAt(indexToRemoveAt);
}
}
public void Clear()
{
HttpSessionState session = GetSessionState();
if (session != null)
{
session["QuestionModelStore"] = null;
}
}
public int Count<T>()
{
HttpSessionState session = GetSessionState();
if (session != null)
{
//return session.Count;
if (session["QuestionModelStore"] != null)
{
var questionModelStore = (List<KeyValuePair<string, T>>)session["QuestionModelStore"];
return questionModelStore.Count;
}
}
return 0;
}
private static HttpSessionState GetSessionState()
{
if (HttpContext.Current != null)
return HttpContext.Current.Session;
return null;
}
}