Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Imagine the following code:

using (SPSite spSite = new SPSite(this.SiteURL))
{
    using (SPWeb spWeb = spSite.OpenWeb())
    {
         using (SPSite spSite2 = new SPSite(this.SiteURL2))
        {
            using (SPWeb spWeb2 = spSite2.OpenWeb())
            {
                //Run a long loop here
            }
        }   
    }
}

It looks really messy to me, but During the "long loop" I reference both sites, and I can't take the performance knock of recreating the SPSite2 and SPWeb2 objects with each iteration in the loop.

If you came across this code in a code review session, do you think it would be acceptable?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You can put multiple assignments in one using-statement.

using (SPSite spSite = new SPSite(this.SiteURL), spSite2 = new SPSite(this.SiteURL2))
using (SPWeb spWeb = spSite.OpenWeb(), spWeb2 = spSite2.OpenWeb()) 
{
  //Run a long loop here
}

Compiled by hand

(I don't know if you can put new SPSite() and spSIte.OpenWeb() in one using, that will further 'clean-up' the code) --> Turns out you can't.

Update: You can only have one type in a using-statement, similar to defining multiple variables of the same type on one line.

And you can optimze further by not adding curly-brackets to the first using-statement.

share|improve this answer
    
That looks great, thank you. –  JL01 Sep 21 '11 at 18:23
    
Tried it doesn't compile. –  JL01 Sep 21 '11 at 19:03
    
I said: compiled by hand, now you know why :) I'll update. –  Dribbel Sep 21 '11 at 19:20
    
Downvoter: care to comment? –  Dribbel Sep 21 '11 at 19:37
    
+1 This is what I'd do, minus the "optimization" of missing out the outer curlies. –  Rawling Sep 22 '11 at 8:31
add comment

I do not see any problems with code like this:

  • The developer understands that he/she must dispose correctly according to the "best practices"
  • It's easy to follow

It's good that the objects are not recreated in the inner "long loop" and this case takes care of when you're having different site collections.

Combining the using statements as Dribbel suggests would likely call to my attention and I would expand them. You'll likely make more mistakes using a composite using statement. And it does not impact performance a bit, you'll save a line or two of codes and risk missing out on a dispose.

share|improve this answer
1  
I agree. The code is self explanatory on what you are doing and I don't see this as a problem. –  Chakkaradeep Chandran Sep 22 '11 at 6:33
add comment

Instead of using 4 usings (!) in this case I would rather use:

try
{
     SPSite spSite = new SPSite(this.SiteURL);
     SPWeb spWeb = spSite.OpenWeb();
     SPSite spSite2 = new SPSite(this.SiteURL2);
     SPWeb spWeb2 = spSite2.OpenWeb()

     // some code
}
catch(Exception e)
{ 
     //Handle exception
}
finally 
{
     if(spWeb2 != null) spWeb2.Dispose();
     if(spSite2 != null) spSite2.Dispose();
     if(spWeb != null) spWeb.Dispose();
     if(spSite != null) spSite.Dispose();
}

It's not perfect but 4 usings + eg. 1 foreach + 1 if .... and you need second monitor just to read code.

If you are reading webs from same site collection then maybe better solution is this:

 //Site url is site collection url
 using (SPSite site = new SPSite(this.SiteURL))
 {
      using (SPWeb web = site.OpenWeb("/url1"))
      {
           using (SPWeb web2 = site.OpenWeb("/url2"))
           {
                //some code
           }
      }
 }

Edited:

After reading @Wictor-SharePointMVP comment I updated code with try catch finally block. Be sure that you always follow Best Practices: Using Disposable Windows SharePoint Services Objects

share|improve this answer
    
This might be dangerous, what if your code throws an exception!? –  Wictor Wilen MCA MCM MVP Sep 22 '11 at 6:12
    
@Wictor-SharePointMVP I don't do exceptions :) Thx for the comment. I really need to be more careful when posting answers. I am always lecturing my colleagues about object disposal and then I make this obvious error (i am a bit ashamed right now) –  Vedran Rasol Sep 22 '11 at 7:08
    
If an exception happens before you initialize one of the SPSite/SPWeb objects, calling .Dispose() on it will also error! –  Neil White Sep 22 '11 at 10:06
    
This SPSE editor is playing with my head. This was not the last version I saved. Thx. Code is now edited (I hope) –  Vedran Rasol Sep 22 '11 at 11:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.