I can't completely understand the reason why we should use "using
" block here
the following code is used to show two tables name "genre" and "review" from the database using EF
it is the code :
C#
protected void Page_Load(object sender, EventArgs e)
{
using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
{
var authorizedReviews = from review in myEntities.Reviews
where review.Authorized == true
orderby review.CreateDateTime descending
select review;
GridView1.DataSource = authorizedReviews.ToList();
GridView1.DataBind();
}
}
here is some explanation from the author :
After the model has been generated, you can execute LINQ queries against it to get data out of the underlying database. To access the data, you need an instance of the
DbContext
class, which is the base class for thePlanetWroxEntities
class. That instance is created inside the Using block in the code. A Using block (using in C#) is used to wrap code that creates a variable that must be disposed of (cleared from memory) as soon as you’re done with it. Because themyEntities
variable holds a (scarce) connection to the SQL Server database, it’s a good idea to wrap the code that uses it in a Using block, so the object is destroyed at the end of the block and the connection is released. ThismyEntities
object then exposes your data (such as reviews and genres) that you can use in a query:
I have two question :
1- Why we should get rid of the variable ? is it because it takes memory space? or is it because it holds a connection to the SQL Server database so we don't need object but just the connection?
2- What does it means "connection is released"? if the connection is part of object why it didn't destroy too?
1) The reason for the using statement is to ensure that the myEntities.Dispose()
method is called as soon as the code inside the using statement completes. This is necessary since the PlanetWroxEntities
class' base class is a DbContext
which holds a connection to the database.
Database connections are a precious resource that are obtained from the database connection pool and there are only a finite number of connections available that your whole application must share. If you don't call Dispose
on the myEntities
object as soon as you are done with it via the using statement (or some other manner) then the myEntities
object will continue to tie up that database connection for some undetermined amount of time until the garbage collection code from the .net memory manager gets around to reclaiming the memory that is held by the myEntities
object that is no longer in use.
The memory manager will at that time calls the dispose method. As soon as the Dispose method is called the database connection is released from the object and returned to the database connection pool where it can be obtained by other parts of your application when you make a call to get a database connection (for example when you create other instances of the PlanetWroxEntities
class.)
The purpose of the using statement is to have the Dispose
method called as soon as you are done with the object so that in this case the database connection is returned to the pool right away.
2) It means the database connection is released by your application and is returned to the database connection pool where it is now available to be obtained by other parts of your application. Here is one resource that talks more about connection pooling, it will give you more background on what is going on under the hood: https://msdn.microsoft.com/en-us/library/8xx3tyca.aspx
Under the hood the using statement is implemented as a finally statement. So this code:
using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
{
var authorizedReviews = from review in myEntities.Reviews
where review.Authorized == true
orderby review.CreateDateTime descending
select review;
GridView1.DataSource = authorizedReviews.ToList();
GridView1.DataBind();
}
becomes
PlanetWroxEntities myEntities;
try{
myEntities = new PlanetWroxEntities();
var authorizedReviews = from review in myEntities.Reviews
where review.Authorized == true
orderby review.CreateDateTime descending
select review;
GridView1.DataSource = authorizedReviews.ToList();
GridView1.DataBind();
} finally{
myEntities.Dispose();
}
In the old days of C# we always had to write the finally statement ourselves to dispose of disposable objects so that their unmanaged resources were released. But later the using statement was introduced in C# to make it easier to accomplish this. Even today, you can write the finally statement yourself if you prefer, but most people love the using statement.
For more information see https://msdn.microsoft.com/en-us/library/yh598w02.aspx