R – Cross Method Dispose Patterns in SharePoint

disposesharepoint

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 Solution

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 the SPSite rather than the SPWeb. I might implement it like this:

public class MyClass : IDisposable
{
    private string _url;
    private SPSite _site;
    private SPWeb _web;

    private SPSite RootSite
    {
        get 
        {
            if ( _site == null )
            {
                _site = new SPSite( _url );
            }

            return _site;
        }
    }

    private SPWeb RootWeb
    {
        get 
        {
            if ( _web == null )
            {
                _web = RootSite.OpenWeb();
            }

            return _web;
        }
    }

    void IDisposable.Dispose()
    {
        if (null != _site)
        {
            _site.Dispose();
        }
    }
}

Note that _web will be automatically disposed by the call to _site.Dispose().

Related Question