I usually like to create the database connection myself and control its lifetime manually with `using{}'. For example:
SqlConnection sqlConnection = new SqlConnection( connectionString );
using( sqlConnection ) {
BusinessObject myBusinessObject = new BusinessObject( sqlConnection );
// do stuff with the business object
...
}
This way it is visible and obvious that I am using a resource that needs to be cleaned up appropriately. However this does end up being a lot of repetitive effort. I'm tempted to create the Sql connection inside the business object and implement IDisposable on it. I would close the connection in the Dispose() method.
using( BusinessObject myBusinessObject = new BusinessObject() ) {
// do stuff with myBusinessObject
...
}
The problem I'm having is that it might not be that obvious that the business object needs to be disposed unless you see it in use.
How would you guys do it?