I have a class with a method performing some database actions.
I want to allow an existing (open) context to be sent in the method call to be used for the database access.
However if a context is not sent, I create a new one.
I just want to make sure the object is not disposed if included in the method call.
Is the object disposed when a using-scope is used in the called method?
// DbService class
class DbService
{
private void SomeDbAction(SomeDbContextObject backendContext = null)
{
using (var context = backendContext ?? CreateNewContextObject())
{
// Some actions using the context
}
}
}
// Call from another class
class Temp
{
void DoSomeThing()
{
var existingContext = new SomeDbContextObject();
dbService.SomeDbAction(existingContext);
// Is dbService disposed here?
UseContextForSomethingElse(existingContext);
}
}
usingstatement took care of that for you.usingcallsDispose(). If you don't wantDispose()to be called, don't use ausingstatement.