public interface IFileStorage
{
void Save(string key);
void Delete(string key);
}
var fileStorage = new FileStorage();
var files = new List<string>();
foreach(var file in files)
{
fileStorage.Save(file);
}
Consider a simple file storage. There is no rollback mechanism whatsoever for cases when app fails in the middle of foreach loop, already saved documents would remain saved.
What would it take to look like this?
using (var transaction = new CustomFileStorageTransaction())
{
var fileStorage = new FileStorage();
var files = new List<string>();
foreach (var file in files)
{
FileStorage.Save(file);
}
}
If error occurs then transaction should automatically performs Delete method on already saved documents within it's scope.
The whole thing can be done with try/catch but I want to be able to build more generic solutions.