那些拥有一次性场的类型应该是一次性的。如何解决这个警告呢? [英] Types that own disposable fields should be disposable. how to solve this warning?
问题描述
我尝试使用的运行代码分析在 2012 VisualStudio的选项
,因为它的结果,我得到了一个警告,因为
I tried using Run Code Analysis option in VisualStudio 2012
, as a result of it I got a warning as
CA1001 Types that own disposable fields should be disposable
Implement IDisposable on 'DBConnectivity'
because it creates members of the following IDisposable types: 'SqlConnection', 'SqlCommand'.
我提到了等等一些问题,但我没听清楚关于 IDisposable的
和下面的类,负责此警告。
I referred some question in SO, but I couldn't catch the point regarding IDisposable
and following is the class, responsible for this warning.
class DBConnectivity
{
public SqlConnection connection = null;
public SqlCommand command = null;
public SqlDataReader dataReader = null;
public string connectionString = null;
public List<MasterTableAttributes> masterTableList;
public DBConnectivity()
{
connectionString = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
connection = new SqlConnection(connectionString.ToString());
//-----Master table results
connection.Open();
string masterSelectQuery = "SELECT * FROM MASTER_TABLE";
command = new SqlCommand(masterSelectQuery, connection);
dataReader = command.ExecuteReader();
masterTableList = new List<MasterTableAttributes>();
while (dataReader.Read())
{
MasterTableAttributes masterTableAttribute = new MasterTableAttributes()
{
fileId = Convert.ToInt32(dataReader["Id"]),
fileName = Convert.ToString(dataReader["FileName"]),
frequency = Convert.ToString(dataReader["Frequency"]),
scheduledTime = Convert.ToString(dataReader["Scheduled_Time"])
};
masterTableList.Add(masterTableAttribute);
}
dataReader.Close();
connection.Close();
}
}
我真的很困惑中实现IDisposable。 ?任何帮助表示赞赏。
I am really confused in implementing the IDisposable. Any help appreciated?
推荐答案
我的编译器完全同意 - 你需要在这里配置你的领域,或(如前所述) - 而不是让他们在第一时间字段:
I fully agree with the compiler - you need to dispose your fields here, or (as already noted) - not make them fields in the first place:
class DBConnectivity : IDisposable // caveat! read below first
{
public void Dispose() {
if(connection != null) { connection.Dispose(); connection = null; }
if(command != null) { command.Dispose(); command = null; }
if(dataReader != null) { dataReader.Dispose(); dataReader = null; }
}
请注意,你会然后通过使用这种类型使用(...)
Note that you would then use this type via using(...)
不过!它看起来像一个静态方法会更合适:
However! It looks like a static method would be more appropriate:
static class DBConnectivity
{
public static List<MasterTableAttributes> GetMasterTableList()
{
var connectionString = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
using(var connection = new SqlConnection(connectionString))
{
connection.Open();
const string masterSelectQuery = "SELECT * FROM MASTER_TABLE";
using(var command = new SqlCommand(masterSelectQuery, connection))
using(var dataReader = command.ExecuteReader())
{
var masterTableList = new List<MasterTableAttributes>();
while (dataReader.Read())
{
MasterTableAttributes masterTableAttribute = new MasterTableAttributes()
{
fileId = Convert.ToInt32(dataReader["Id"]),
fileName = Convert.ToString(dataReader["FileName"]),
frequency = Convert.ToString(dataReader["Frequency"]),
scheduledTime = Convert.ToString(dataReader["Scheduled_Time"])
};
masterTableList.Add(masterTableAttribute);
}
return masterTableList;
}
}
}
}
或也许像一个工具,简单的短小精悍:
or perhaps simpler with a tool like "dapper":
static class DBConnectivity
{
public static List<MasterTableAttributes> GetMasterTableList()
{
var connectionString = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
using(var connection = new SqlConnection(connectionString))
{
connection.Open();
const string sql = "SELECT Id as [FileId], FileName, Frequency, Scheduled_Time as [ScheduledTime] FROM MASTER_TABLE";
return connection.Query<MasterTableAttributes>(sql).ToList();
}
}
}
这篇关于那些拥有一次性场的类型应该是一次性的。如何解决这个警告呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!