I usually like to create a database connection myself and manually control its lifetime using `using {} '. For example:
SqlConnection sqlConnection = new SqlConnection( connectionString );
using( sqlConnection ) {
BusinessObject myBusinessObject = new BusinessObject( sqlConnection );
...
}
Thus, it is visible and obvious that I am using a resource that needs to be cleaned accordingly. However, this leads to many repetitive efforts. I am tempted to create an Sql connection inside a business object and implement IDisposable on it. I would close the connection in the Dispose () method.
using( BusinessObject myBusinessObject = new BusinessObject() ) {
...
}
The problem I am facing is that it may not be so obvious that the business object should be deleted if you do not see it in use.
How do you guys do this?