How can I share IDisposable resources inside a class?

I am writing several console applications to move some data between SQLite databases. The classes for the connection and the various implenent IDisposable prepared statements, so I create these objects using blocks using

like:

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 ways. What's the cleanest and least confusing way to store these connections as instance variables? I want their methods to be .Dispose()

called in an intelligent way, but can't see a good way to make sure all the action happens in the context of one block using

.

I understand this is a C # newb question, so please attach your answers as such. If you have a suggested solution, I would love it if you included sample code to illustrate.

EDIT: My use case is a console app. Someone navigates to the source and destination strings and the console application performs the operation. Can I actually implement my IDisposable console class program like this:

class Program : IDisposable
{
    private SQLiteConnection sourceConnection;
    private SQLiteConnection destinationConnection;

    public void Dispose()
    {
        sourceConnection.Dispose();
        destinationConnection.Dispose();
    }  
}

      

+1


a source to share


3 answers


I need to be able to create these connections in one method and then call them in other ways. What's the cleanest and least confusing way to store these connections as instance variables?

In this case, the class you are storing these instance variables in must implement IDisposable itself, and you must make sure the connections are located when you delete.



Also, when you have multiple IDisposables at once, you can rewrite it this way, group them and reduce nesting in your 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

      

+2


a source


You can force your class to implement IDisposable and provide connection cleanup, etc. inside your Dispose method. Then the method calling your class can use (MyClass c ...).



+1


a source


One way is to implement your IDisposable class. The Dispose method of the class calls the Dispose method of the connection and the command, etc. Then you can use an instance of your class in the block you use.

+1


a source







All Articles