Cross Dispose Patterns method in SharePoint

I wrote a class that performs various modifications to the contents of a SharePoint site. Inside this class, I implemented a lazy resolved property

    private SPWeb rootSite
    {
        get 
        {
            if ( _site == null )
            {
                SPSite site = new SPSite( _url );
                _site = site.OpenWeb();
            }

            return _site;
        }
    }

Both SPSite and SPWeb must be removed, and according to the Best Practices document, this situation is called the Cross Method Dispose Pattern ... only they do not give any actual suggestions on how to implement the recycling of part of the template.

I decided that my class implements IDisposable (placing the website and website there) and the recipient gets access to it through the use clause. Would this be in line with “best practices”, or would I have to solve the problem differently?

, , , , , .

+4
1

, "Cross Method Dispose Pattern" " ". , SPSite SPWeb , , . , , SharePoint.

, , IDisposable - . SPSite, SPWeb. :

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();
        }
    }
}

, _web _site.Dispose().

+4

All Articles