D
Daniel Billingsley
Given that
- The CloseConnection CommandBehavior of ExecuteReader causes the connection
to be "closed" when the DataReader is closed
- Close and Dispose are interchangeable on these ADO objects
1) Will the connection object in the following code get closed/disposed?
2) Since it's created in the static method of the DataFactory class but
referenced by the DataReader in the BusinessObject class, how would you
describe the scope of the connection object?
Wait - variables have scope, not objects in memory, right? So it will be
referenced both by the DataReader in the BusinessObject and the variable cn
within the scope of the GetDataReader method, right? We know when the cn
reference goes away, of course... so when the DataReader is closed then the
connection will also be closed and all will be right in the GC world.
Yes???
If possible, can you enlighten me on how you'd go about testing such a
thing, if it's possible. The DataReader doesn't expose any information
about the connection so it can't be checked through the back door that way.
============
class BusinessObject
{
public ReadData
{
using (SqlDataReader dr = DataFactory.GetDataReader()) // has some
parameters in reality
{
// extract the data from the datareader here
}
}
}
class DataFactory
{
public static SqlDataReader GetDataReader() // takes some parameters in
reality
{
SqlDataReader dr = null;
SqlConnection cn = new SqlConnection(); // with necessary parameters
cn.Open();
using (SqlCommand cm = new SqlCommand()) // with necessary parameters
{
// Set up command object
dr = cm.ExecuteReader(CommandBehavior.CloseConnection);
}
return dr;
}
}
- The CloseConnection CommandBehavior of ExecuteReader causes the connection
to be "closed" when the DataReader is closed
- Close and Dispose are interchangeable on these ADO objects
1) Will the connection object in the following code get closed/disposed?
2) Since it's created in the static method of the DataFactory class but
referenced by the DataReader in the BusinessObject class, how would you
describe the scope of the connection object?
Wait - variables have scope, not objects in memory, right? So it will be
referenced both by the DataReader in the BusinessObject and the variable cn
within the scope of the GetDataReader method, right? We know when the cn
reference goes away, of course... so when the DataReader is closed then the
connection will also be closed and all will be right in the GC world.
Yes???
If possible, can you enlighten me on how you'd go about testing such a
thing, if it's possible. The DataReader doesn't expose any information
about the connection so it can't be checked through the back door that way.
============
class BusinessObject
{
public ReadData
{
using (SqlDataReader dr = DataFactory.GetDataReader()) // has some
parameters in reality
{
// extract the data from the datareader here
}
}
}
class DataFactory
{
public static SqlDataReader GetDataReader() // takes some parameters in
reality
{
SqlDataReader dr = null;
SqlConnection cn = new SqlConnection(); // with necessary parameters
cn.Open();
using (SqlCommand cm = new SqlCommand()) // with necessary parameters
{
// Set up command object
dr = cm.ExecuteReader(CommandBehavior.CloseConnection);
}
return dr;
}
}