C
Carl R
I have a class that owns a dataset, adding to it during it's work.
The method using the class fetches the dataset in order to return it
as a result in a web method.
Simplified:
class A
{
Dataset m_b;
public A()
{m_b=new Dataset();}
public DoWork()
{ /* appends to dataset m_b */}
public Dataset Result
{ return m_b;}
}
Allright, since A owns a dataset, according to fxcop it should
implement IDisposable.
class A: IDisposable
{
Dataset m_b;
public A()
{m_b=new Dataset();}
public DoWork()
{ /* appends to dataset m_b */}
public Dataset Result
{ return m_b;}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
m_b.Dispose();
}
}
}
If I use the class like this:
[WebMethod]
Dataset DoStuff()
{
Dataset d;
using(A a = new A())
{
a.DoWork();
d = a.Result;
}
//d is already disposed here
return d;
}
Then A would dispose the dataset resulting in d being invalid on the
last row.
Ok, so if I change A to return a copy this problem is solved right?
class A: IDisposable
{
public Dataset Result
{ return m_b.Copy();}
}
Now what happens with the dataset that I return in the webservice?
The serialized dataset is up to the retriever, but the one on my side
can hardly be disposed automatically? (only finalized)
Alternatives:
[WebMethod]
Dataset DoStuff()
{
Dataset d;
using(A a = new A())
{
a.DoWork();
d = a.Result;
}
try
{
return d;
}
finally
{
d.Dispose();
}
}
[WebMethod]
Dataset DoStuff()
{
using(Dataset d)
{
using(A a = new A())
{
a.DoWork();
d = a.Result;
}
return d;
}
}
[WebMethod]
Dataset DoStuff()
{
using(A a = new A())
{
a.DoWork();
Using(Dataset d = a.Result)
{
return d;
}
}
}
Do you find theese alternatives reasonable? I think they look rather
strange all of them.
What is the correct way?
("Dataset" can be anything IDisposable.
d can be used between retrieving it and returning it.)
Thanks!
The method using the class fetches the dataset in order to return it
as a result in a web method.
Simplified:
class A
{
Dataset m_b;
public A()
{m_b=new Dataset();}
public DoWork()
{ /* appends to dataset m_b */}
public Dataset Result
{ return m_b;}
}
Allright, since A owns a dataset, according to fxcop it should
implement IDisposable.
class A: IDisposable
{
Dataset m_b;
public A()
{m_b=new Dataset();}
public DoWork()
{ /* appends to dataset m_b */}
public Dataset Result
{ return m_b;}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
m_b.Dispose();
}
}
}
If I use the class like this:
[WebMethod]
Dataset DoStuff()
{
Dataset d;
using(A a = new A())
{
a.DoWork();
d = a.Result;
}
//d is already disposed here
return d;
}
Then A would dispose the dataset resulting in d being invalid on the
last row.
Ok, so if I change A to return a copy this problem is solved right?
class A: IDisposable
{
public Dataset Result
{ return m_b.Copy();}
}
Now what happens with the dataset that I return in the webservice?
The serialized dataset is up to the retriever, but the one on my side
can hardly be disposed automatically? (only finalized)
Alternatives:
[WebMethod]
Dataset DoStuff()
{
Dataset d;
using(A a = new A())
{
a.DoWork();
d = a.Result;
}
try
{
return d;
}
finally
{
d.Dispose();
}
}
[WebMethod]
Dataset DoStuff()
{
using(Dataset d)
{
using(A a = new A())
{
a.DoWork();
d = a.Result;
}
return d;
}
}
[WebMethod]
Dataset DoStuff()
{
using(A a = new A())
{
a.DoWork();
Using(Dataset d = a.Result)
{
return d;
}
}
}
Do you find theese alternatives reasonable? I think they look rather
strange all of them.
What is the correct way?
("Dataset" can be anything IDisposable.
d can be used between retrieving it and returning it.)
Thanks!