I have some legacy code that execute an Http API call on Dispose, this class read some data from an API and permits the manipulation, the save of the manipulated data is executed in the Dispose method calling an API.
Example "pseudo" code:
public Class Manipulator : IDisposable
{
public Manipulator(){}
public void Load(int ID){ ... call API to load data ... }
public void Manipulate1 { ... manipulate data locally ...}
...
public void ManipulateN { ... manipulate data locally ...}
public void Save(){ ... call API to save data ... }
protected override void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
Save(); // Call and Http API to save the data
}
_disposed = true;
}
}
}
}
and the code is used as:
using(var data = new Manipulator())
{
data.load(123);
data.manipulate(...)
}// Saved on dispose
Ignoring exception that are intercepted etc... Is this an acceptable pattern? or it is considered a bad practice? alternatives?