Let's say I have a class that has a MyCustomDatabaseAccess as a data member. MyCustomDatabaseAccess has a Dispose() method. MyCustomDatabaseAccess is the middleware class that accesses the database.
public class MyClass {
private MyCustomDatabaseAccess db_access;
}
Does MyClass need to implement IDisposable interface?
My solution right now is to have do something like this:
public class MyClass {
private MyCustomDatabaseAccess db_access;
public void GetDBResults () {
db_access = new MyCustomDatabaseAccess();
DataTable dt = db_access.ExecuteStoredProc(param1, param2, etc..);
//do stuff with results
db_access.Dispose();
}
}
From what I read on MSDN, another way to make sure that this object is disposed of properly would be to have MyClass implement IDisposable interface, then implement a Dispose() function, then call it in the class that calls an object of MyClass. see this for more info http://www.devx.com/dotnet/Article/33167/0/page/3
Which way is preferable and why? thanks!