none
Disposing and good patterns RRS feed

  • Question

  • I've been doing some database work in C# recently and would like some advice about improving my code.  I've bought a book and looked at many online examples/tutorials, but most of them don't dispose the objects.  It makes the code much simpler, but I'm sure it must cause problems, especially in server software that rarely stops.  When I create code for accessing a database I usually do something like:

     

    Code Snippet

    using (SqlConnection connection = new SqlConnection("dummy connection string"))

    {

    using (SqlCommand command = new SqlCommand(/*parameters*/))

    {

    // Code...

    using (SqlDataReader reader = command.ExecuteReader())

    {

    // Code...

    }

    }

    }

     

     

    There are a few things I don't like about this.  Firstly there are a quite a few indents, especially if you wrap it in a try block, and have an if or two to reader from the reader.  It also seems like a lot of code to have in one function.  What I've got at the moment isn't too bad, but there's still quite a bit of code to add.  A separate function could be used to process the reader though.

     

    try...catch...finally could be used to get rid of the indenting, but then there's a greater possiblity of forgetting to dispose something, and it takes up more space checking for nulls etc.

     

    Is this the best way to use databases or is there a much neater way?  Datasets may be a possibility, but I'm not sure that they'll help much, and I haven't had much experience with them.

    Friday, September 21, 2007 8:10 PM

Answers

  • Imho the using statement is a "good pattern". The statement was "invented" as a shortcut to keep you from writing code like:

    Code Snippet

    Instance i = new Instance();
    try
    {
     //
    }
    finally
    {
     if (i != null)
     {
      i.Displose();
     }
    }


    Btw, you could avoid "an indent" as following:

    Code Snippet

    using (SqlConnection conn = new SqlConnection("xxx"))
    using (SqlCommand cmd = conn.CreateCommand())
    {
     // Code...
     using (SqlDataReader reader = cmd.ExecuteReader())
     {
     }
    }



    Saturday, September 22, 2007 8:01 AM

All replies

  • Imho the using statement is a "good pattern". The statement was "invented" as a shortcut to keep you from writing code like:

    Code Snippet

    Instance i = new Instance();
    try
    {
     //
    }
    finally
    {
     if (i != null)
     {
      i.Displose();
     }
    }


    Btw, you could avoid "an indent" as following:

    Code Snippet

    using (SqlConnection conn = new SqlConnection("xxx"))
    using (SqlCommand cmd = conn.CreateCommand())
    {
     // Code...
     using (SqlDataReader reader = cmd.ExecuteReader())
     {
     }
    }



    Saturday, September 22, 2007 8:01 AM
  •  

    indeed, the using statement disposes the object for you rather than calling the Dispose() method manually.
    Sunday, September 23, 2007 10:13 AM
  • Thanks for the reply.  Is it a good idea to avoid the extra indent?  Is that kind of thing a standard way of indenting?

     

    When ever I have something like an if/switch/using/try I always indent it, and have never considered not indenting.  Your example if fairly easy to read, but are there people out there who hate this kind of thing?

    Sunday, September 23, 2007 4:41 PM
  • It doesn't really matter to the compiler about indenting but to the developer/reader. It is easier and clearer to read and VS.NET automatically indents code when you say, close an opening brace.

     

    Sunday, September 23, 2007 4:44 PM