如何在类中共享 IDisposable 资源?
我正在编写一些控制台应用程序来在 SQLite 数据库之间移动一些数据。 连接的类和各种准备好的语句是 IDisposable 实现的,因此我使用 using
块实例化这些对象,如下所示:
using (SQLiteConnection sqlite_conn = new SQLiteConnection(connectionString))
{
sqlite_conn.Open();
using (SQLiteCommand sqlite_cmd = new SQLiteCommand())
{
sqlite_cmd.Connection = sqlite_conn;
sqlite_cmd.CommandText = "INSERT INTO SOMETHING SOMETHING;";
sqlite_cmd.ExecuteNonQuery();
}
sqlite_conn.Close();
}
但是,我需要能够在一种方法中创建这些连接,然后用其他方法调用它们。 对我来说,将这些连接存储为实例变量的最干净、最不混乱的方法是什么? 我想确保他们的 .Dispose()
方法以智能方式调用,但找不到一个好的方法来确保所有操作都发生在单个 的上下文中使用
块。
我意识到这是一个 C# 新手问题,因此请调整您的答案。 如果您有建议的解决方案,如果您包含一个代码示例来说明,我会很高兴。
编辑:我的用例是控制台应用程序。 有人传入源和目标连接字符串,控制台应用程序执行操作。 我真的会让我的控制台类 Program 本身实现 IDisposable 吗?:
class Program : IDisposable
{
private SQLiteConnection sourceConnection;
private SQLiteConnection destinationConnection;
public void Dispose()
{
sourceConnection.Dispose();
destinationConnection.Dispose();
}
}
I'm writing some console applications to move some data between SQLite databases. The classes for the connection and various prepared statements implenent IDisposable, so I'm instantiating these objects using using
blocks, like this:
using (SQLiteConnection sqlite_conn = new SQLiteConnection(connectionString))
{
sqlite_conn.Open();
using (SQLiteCommand sqlite_cmd = new SQLiteCommand())
{
sqlite_cmd.Connection = sqlite_conn;
sqlite_cmd.CommandText = "INSERT INTO SOMETHING SOMETHING;";
sqlite_cmd.ExecuteNonQuery();
}
sqlite_conn.Close();
}
But, I need to be able to create these connections in one method, and then call them in other methods. What is the cleanest and least confusing way for me to store these connections as instance variables? I want to make sure their .Dispose()
methods are called in an intelligent way, but can't see a good way to make sure all of the action occurs within the context of a single using
block.
I realize this is a C# newb question so please tailor your answers as such. If you have a suggested solution, I'd love it if you included a code sample to illustrate.
EDIT: My use case is a console app. Someone passes in the source and destination connection strings, and the console app performs an operation. Would I actually make my console class Program itself implement IDisposable like this?:
class Program : IDisposable
{
private SQLiteConnection sourceConnection;
private SQLiteConnection destinationConnection;
public void Dispose()
{
sourceConnection.Dispose();
destinationConnection.Dispose();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,保存这些实例变量的类本身应该实现 IDisposable,并且在处置时应确保连接也已处置。
另外,当您同时拥有多个 IDisposable 时,您可以通过这种方式重写它,将它们分组并减少代码中的嵌套:
In this case, the class where you keep these instance variables should itself implement IDisposable, and on disposal should make sure the connections are also disposed.
Also, when you have multiple IDisposables at once you can re-write it this way, to group them and reduce the nesting in the code:
您可以使您的类实现 IDisposable 并确保在 Dispose 方法中清理连接等。 然后调用你的类的方法可以使用(MyClass c ...)。
You can make your class implement IDisposable and ensure you clean up the connection etc within your Dispose method. Then the method calling your class can do using(MyClass c ...).
一种方法是让您的类实现 IDisposable。 在类的 Dispose 方法中,调用连接和命令等的 Dispose 方法。然后您可以在 using 块中使用类的实例。
One way is to have your class implement IDisposable. In your class's Dispose method, call the Dispose methods of the connection and the command etc. Then you can use an instance of your class in a using block.