0
public class MovieModel 
{
    public string id { get; set; }
    public string title { get; set; }
    public string image { get; set; }
    public string cnt { get; set; }        
}

public class DataSetHolder
{
    public DataSet Data = new DataSet();
    public Hashtable DataAdapters = new Hashtable();
    public SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
}

Do I need to implement the IDisposable interface for both the classes?

Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
sly_Chandan
  • 3,437
  • 12
  • 54
  • 83
  • Why do you think that `MovieModel` might need `IDisposable`? – Philip Kendall Nov 05 '13 at 09:40
  • Because MovieModel is a reference type and it needs to be disposed explicitly – sly_Chandan Nov 05 '13 at 09:41
  • To quote from [Implementing a Dispose Method](http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx): "The dispose pattern is used only for objects that access *unmanaged* resources." (my emphasis). I think you probably need to do some more reading about what `IDisposable` is for. – Philip Kendall Nov 05 '13 at 09:43

2 Answers2

1

You don't need to implement IDisposable for MovieModel. The garbage collector will take care of it. You might implement IDisposable for DataSetHolder. Please read the IDisposable documentation from MSDN to see how and when IDisposable should be used.

The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
1

You implement IDispose:
a. Because you have UNmanaged resource (raw file handles etc) that you need to free - a very rare case. OR
b. Because you want to explicitly control access lifetime to resource controlled by managed objects.

Neither a. or b. apply to MovieModel. It does not contain any disposable objects that access resources you want control the lifetime off. No IDisposable implementation necessary.

For DataSetHolder a. does not apply, b. however may because it holds an SqlConnection object that manages a resource (the db connection). This is quite particular case because that resource is pooled. You could provide a mimimal IDisposable implementation and in your Dispose just dispose the connection (returning it to the connection pool). That would enable users of DataSetHolder to dispose it manually or to make use of a "using" block to control the lifetime of the connection.

public class DataSetHolder : IDisposable {
    ...
    void Dispose() {
        if (connection!=null)
            connection.Dispose();
    }
}

It may however (see here) be better just to ensure within DataSetHolder that when ever you use the connection you close it when done (i.e by wrapping all useage of the connection within DataSetHolder in a using statement). That way you are not holding the connection away from the pool unnecessarily. If the connection is freed in this manner on EVERY use then there is no need to implement IDispose (and app will scale better).

public class DataSetHolder {
    ...  
    void DoSomething() {
       using (connection) {
           ...
       }
    }

    void DoSomethingElse() {
       using (connection) {
           ...
       }
    }

    // No need for Dispose - the connection is disposed each time we use it.
}
Community
  • 1
  • 1
Ricibob
  • 7,505
  • 5
  • 46
  • 65