I've written a class that does various modification to the contents of a SharePoint site. Inside that class, I've implemented a lazy resolved property
private SPWeb rootSite
{
get
{
if ( _site == null )
{
SPSite site = new SPSite( _url );
_site = site.OpenWeb();
}
return _site;
}
}
Both the SPSite and the SPWeb need to be disposed, and according to the Best Practices document this situation is called a Cross Method Dispose Pattern… only they don't give any actual suggestion on how to implement the dispose part of the pattern.
I opted to have my class implement IDisposable (disposing the site and web there), and have the caller access it through a using clause. Would that be according to "best practices", or should I have handled the problem differently?
Note that I come from a strict reference-counting background, so please correct me if my views on garbage disposal are a bit off.
Best Answer
I consider the "Cross Method Dispose Pattern" to be the worst of the "Best Practices". It is almost always better to use dependency injection to provide an SPSite or SPWeb reference for your class to use, perhaps through the constructor. That way your class has no disposal concerns, it just consumes the SharePoint context.
That said, if you wish to proceed with this pattern, implementing
IDisposable
is the correct approach. However, you should track and dispose theSPSite
rather than theSPWeb
. I might implement it like this:Note that
_web
will be automatically disposed by the call to_site.Dispose()
.