Scenario (note this is hand written dummy code - might not work :)
public class MySuperClass
{
private static Dictionary<int, MySuperClass> _instances = new Dictionary<int, MySuperClass>
private SPWeb _contextWeb;
private MySuperClass(SPWeb web)
{
// construct me
_contextWeb = web;
}
public static GetSuperClass(int id)
{
if (_instances.ContainsKey(id))
{
return _instances[id];
}
var instance = new MySuperClass(SPContext.Current.Web);
_instances.Add(22, instance);
}
}
public class SampleApplication
{
int _id = 22;
public void Run()
{
// get MySuperClass
var msc = MySuperClass.GetMySuperClass(22);
msc.SomeOperationOnItsContextWeb(); // like reading from the PropertyBag..
}
}
The sum of MySuperClass instances (inside the static field) will be limited (as in "not indefinite"). Once an instance is put there for a certain "id" it will be reused over and over. The question is... if I run any operations from within my MySuperClass on my _contextWeb - will they at some point fail?
I would prefer to have this specific SPWeb in memory because I don't want to create a new SPWeb instance all the time (= every request). That's why the whole MySuperClass is stored in a static field anyway. We are talking about a Web Publishing Scenario with 500 concurrent users (easily). So that would be expensive.
I am also totally aware that any contextWeb would not ever been garbage collected right now. But since I'm in control of all my MySuperClass instances I could call Dispose() on them (not implementing IDisposable in the example above).