SP - What is the reason to use “using” in WebParts
-
11-10-2020 - |
题
I want to know, what is the reason to use "Using" like this:
using (SPSite oSite = new SPSite("www.contoso.com"))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
**my code here......**
}
}
How is the best practice for naming my objects (oSite and oWeb). It is curious :)
解决方案
First of all, it doesn't have any to do with your code begin run in a web part (or any other specific position).
What really matters here is that you are creating a new instance of a SPSite
/SPWeb
object. Since these classes both implement the IDisposable
interface, and since you are creating the instances yourself (in contrast to getting them from another object, for example SPContext.Current.Web
to get the current web site) you are also responsible of handling the disposal of the instances you created.
As far as naming guidelines go, I don't think there is any SharePoint-specific guidance you should be aware off, so you probably should just stick to the generic naming guidelines provided by Microsoft, which means you should probably use camelCase for local variables names like these. As a side note, I would usually add the prefix "elevated" to the variable name if the instance was created in a SPSecurity.RunWithElevatedPrivileges
context, to remind me that any action I will perform on it will be performed under elevated privileges.
其他提示
The reason for using using
is partly correct by the other answers. using
is shorthand for try
...finally
and used on objects implementing IDisposable
(more on this in a bit) the dispose part is done automatic for you. See more here about the using
statement: https://msdn.microsoft.com/en-us/library/yh598w02.aspx
But why do SPSite
, SPWeb
and other objects in SharePoint implement the IDisposable
interface. All these objects are not only managed code, they are implemented in unmanaged code. As the matter of fact the real logic for SPSite
and SPWeb
are implemented using COM+. COM+ is unmanaged, meaning you have to manage all references yourself and clean up afterwards, to avoid memory and resource leaks. So whenever you use SPSite
for instance you are creating a managed object in .NET that uses an unmanaged object (COM+) and the garbage collector in .NET does not know how to handle that. Therefore these objects implements the IDisposable
interface which has a method called Dispose()
that should be used whenever you do not need the object anymore. And this is exactly what happens with that using
statement, it ensures that Dispose()
is called on your object and relieves you of memory and resource leaks.
The benefit of using using
in code is primarily that you don't have to think about freeing memory when your done with the object. If you don't use using
you have to dispose every object, and that can sometimes be difficult to remember when your programming.
I've seen several occasions where you iterate all webs in a site and eventually taking down a SharePoint server. So you should continue to use using
to make sure that you don't eat all the memory in your code.
These Site
and Web
objects take up lots of memory and needs to be released at the end of the usage. This is also taken care by the CLRs garabage collector
.
However it would be best practice if you as a developer take steps to dispose/release the memory after usage of the object. This is were you use Dispose method
or Using
for the IDosposiable objects.
Note - its not advicable to blindly dispose objects, for example you dont dispose a web/site object inside a Event Reciever if the object is reference from the properties parameter.
For more on the best practices please go through following documents