Reputation: 2335
I would like to optionally dispose (with a using statement) an object. If the connection string is passed in the constructor, then the using must dispose and if the object SQLDataLayer is passed in the constructor, then I don't want to dispose. This is to avoid passing the SQLDataLayer object in every re-usable method, is this the right approach?
public class RepoCategory
{
readonly string connString;
SQLDataLayer dl = null;
public RepoCategory(string conn_string)
{
this.connString = conn_string;
}
public RepoCategory(SQLDataLayer dl)
{
this.dl = dl;
}
//I WANT TO MAGICALLY generate using(null) if dl was passed in constructor
//or using(dl = new SQLDataLayer(connString)) if connString was passed.
public Category Get(int category_sid)
{
Category cat = null;
using (SQLDataLayer dlDispose = (dl == null) ? new SQLDataLayer(connString) : null)
{
//can this be simplified/avoided?
if (dlDispose != null)
dl = dlDispose;
cat = dl.GetCat();
}
return cat;
}
//I DON'T WANT TO PASS THE DL MANUALLY EVERYTIME
public Category Get(SQLDataLayer dl, int category_sid)
{
Category cat = null;
cat = dl.GetCat();
return cat;
}
}
thanks!
Upvotes: 0
Views: 146
Reputation: 283674
In your particular example, like this:
SQLDataLayer dlDispose = null;
SQLDataLayer dl = this.dl ?? (dlDispose = new SQLDataLayer(connString));
using (dlDispose) { dl.Blah(...) }
This also fixes the bug where you leave the class field referring to a short-lived connection that you're also disposing.
For more general usefulness, it is helpful to have a holder class that implements Dispose and will conditionally call Dispose on the object it holds.
Upvotes: 0
Reputation: 887453
You don't need to declare variables in a using
statement.
You can make a using
statement that only does anything in some circumstances:
SQLDataLayer dl = ...;
using(someCondition ? dl : null) {
...
}
Upvotes: 0
Reputation: 50190
using is just short hand for
try
{
obj = new Obj();
}
finally
{
obj.Dispose();
}
You can call Dispose directly if you want. Just do try / finally and in the finally go if(xxx) obj.Dispose()
Upvotes: 1