Hello,
Is it appropriate to use the CallContext class for storing
request-specific data? I have a set of business objects that
I'm building. Each class contains a hashtable that maps from a key to
an object. Whenever a constructor is called, the hashtable is
consulted first to see if an object with that key already exists. If
it doesn't, the object is pulled from the database and put inside of
the hashtable. I'd like the hashtables to be specific to an execution
context -- for instance, if two different web requests create a Person
object with an ID of 100, I'd like them to get 2 different objects.
But within one executing web request, if a Person object with an ID of
100 is created twice, I'd like the same object to be returned both
times.
The business objects might be used outside of a web context, so I
can't rely on the HttpContext.Items collection -- would CallContext be
an appropriate alternative? It resides in the
System.Runtime.Remoting.Messaging namespace, which gives me pause.
I've copied below the two relevant methods for fetching and storing an
object. Thanks in advance for any help!
public static Person GetByKey(int id)
{
object o = CallContext.GetData("PersonMap");
if (o != null)
{
Hashtable map = (Hashtable)o;
return map[id] as Person;
}
else
return null;
}
public static void AddToMap(Person person)
{
if (CallContext.GetData("PersonMap") == null)
{
Hashtable map = new Hashtable();
map.Add(person._id, person);
CallContext.SetData("PersonMap", map);
}
else
{
Hashtable map = (Hashtable)CallContext.GetData("PersonMap");
map.Add(person._id, person);
}
}