When creating my SharePoint 2010 solutions, I often create a static class that contains helper methods and lists, documents, list items, etc that I use frequently. What would the dangers of putting my SPWeb object in there? Or SPSite for that matter?
My static class would contain a method like:
public static SPWeb webObject;
public static SPWeb WebObject()
{
if (webObject != null)
return webObject;
else if (SPContext.Current != null)
{
webObject = SPContext.Current.Web;
}
return webObject;
}
That way I can populate the webObject at any time, or just call on the method which would return the webObject or SPContext.Current.Web if webObject is null.
So instead of writing my code like:
SPWeb web = site.RootWeb;
SPList list = web.Lists[listGuid];
It would look like:
SPList list = SharePointState.WebOject().Lists[listGuid];
Thus saving me an extra line of code and I wouldn't have to constantly be initializing a SPWeb object.
Now, disposing of the SPWeb object would make this idea pointless since the static method would just be recreating the SPWeb object every time I called on it. Would this crazy idea drain resources and make my solution perform slowly? What are some of the dangers of doing this?