如何在类中共享 IDisposable 资源?

发布于 2024-07-20 07:28:07 字数 1136 浏览 4 评论 0原文

我正在编写一些控制台应用程序来在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-07-27 07:28:07

我需要能够在一种方法中创建这些连接,然后在其他方法中调用它们。 对我来说,将这些连接存储为实例变量的最干净、最不混乱的方法是什么?

在这种情况下,保存这些实例变量的类本身应该实现 IDisposable,并且在处置时应确保连接也已处置。

另外,当您同时拥有多个 IDisposable 时,您可以通过这种方式重写它,将它们分组并减少代码中的嵌套:

using (SQLiteConnection sqlite_conn = new SQLiteConnection(connectionString))
using (SQLiteCommand sqlite_cmd = new SQLiteCommand())
{
    sqlite_conn.Open();

    sqlite_cmd.Connection = sqlite_conn;
    sqlite_cmd.CommandText = "INSERT INTO SOMETHING SOMETHING;";
    sqlite_cmd.ExecuteNonQuery();
} // no need to call .Close(): IDisposable normally handles it for you

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?

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:

using (SQLiteConnection sqlite_conn = new SQLiteConnection(connectionString))
using (SQLiteCommand sqlite_cmd = new SQLiteCommand())
{
    sqlite_conn.Open();

    sqlite_cmd.Connection = sqlite_conn;
    sqlite_cmd.CommandText = "INSERT INTO SOMETHING SOMETHING;";
    sqlite_cmd.ExecuteNonQuery();
} // no need to call .Close(): IDisposable normally handles it for you
雨的味道风的声音 2024-07-27 07:28:07

您可以使您的类实现 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 ...).

愛放△進行李 2024-07-27 07:28:07

一种方法是让您的类实现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文