locked
Dispose Pattern? RRS feed

  • Question

  • Hi everyone i have a general dispose pattern question. When your designing an object with a dispose is there anything that takes care of the following situation:

     

    public void InternalFx(SQLConnection con)

     

    {

     

    }

     

    //calling code

    MyClass.InternalFx(new SQLConnection(ConnectionString));

     

     

    Ok so here is my question. Does the SQLConnection ever get disposed? Sure the finalizer would take care of it but, in most cases, i don't think you would want to wait for the finalizer. It would be dangerous if you would try to dispose it in your class because it could be called like this:

    SQLConnection con = new SQLConnection(ConnectionString));

    MyClass.InternalFx(con);

     

    In this case you wouldn't want to dispose it internally in the class but in the second case you would. Back in the COM days you could just look at the reference counter to make this decision but since there isn't anything like this in .Net how does this work? Does the recommended dispose pattern take care of this situation?

     

    thanks,

    ncage

    Friday, August 24, 2007 5:37 PM

Answers

All replies

  • Maybe I'm off here, but if SQLConnection implements IDisposable then, I'd recommend using the "using" statement as follows:

    // Calling Code
    using (SQLConnection conn = new SQLConnection(ConnectionString))
    {
         MyClass.InternalFx(conn);
    }

    At the end of the using statement, the Dispose method of the IDisposable interface is called on the SQLConnection, thus closing your connection.  The "using" keyword can be used with anything implementing the IDisposable interface.
    Friday, August 24, 2007 7:31 PM
  • That is correct for SQL connections, DataReader and DataSet you can use the second Using statement which call Dispose for you automatically on classes that implements IDisposable Interface.  But if you are using the streams which uses unmanaged resources you need a dispose pattern which calls Dispose Bool.  Try the link below for details.

     

    http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx

     

    Friday, August 24, 2007 8:57 PM
  • Sorry guys i guess i was a little unclear with my question. I was asking from the point of designing classes that other people are using. Yes i know what dispose is and i know what dispose patterns are but my question was from the point of class design. So if are designing a class that accepts any type of object as parameter that is either a class level variable or local variable to that class and has a dispose method how will that dispose method ever get called if the user of your class doesn't something like this by anything other than the finalizer
    MyClass.MyFunction(New SQLConnection(MyConString));
    ?

    Does it make more sense what im asking now?

    Saturday, August 25, 2007 1:56 AM
  • If the person starts the code block with the second using statement the clr determines when dispose is called, which means you don't know when it will be called in C#/VB 2.0.  Now I see what you mean but you may get some answers below.

     

    http://blogs.msdn.com/andrewdownum/archive/2005/01/04/346448.aspx

     

    Saturday, August 25, 2007 2:14 AM
  • That's an issue of how you design your API, very little to do with the dispose pattern per se. If you allow an object to be passed into your API then you have no control over it. If they choose not to dispose then that's up the caller. I suppose you could rewrite your API to something like ExecuteAndCloseConnection(Connection) and then you could dispose it but really you shouldn't be touching their passed in object.

     

    Monday, October 29, 2007 5:22 PM