My program needs to treat some file operations as a unit, some of the important steps in a case are:
- delete a file in a directory.
- validate some data. if the data is invalid, roll back.
- Upload a file to the directory. if upload failed , roll back.
- commit changes.
I raise this requirement about transaction is because sometimes a user may use VPN to access my website, the VPN connection maybe very unstable.
If the 3rd step faild, the 1st file and the new uploaded file will all LOST.
some of my code:
DeleteTheOlderDT(path, FileName);
this.fuTemplateName.SaveAs(path + FileName);
_t.FileName = FileName;
if (!System.IO.File.Exists(path + FileName))
{
ArrayList ItemList = new ArrayList();
ItemList.Add("Uploading failed, please upload again!");
btlError.DataSource = ItemList;
btlError.DataBind();
_t.FileName = "";
return;
}
I think you can only do this manually. Look at UnitOfWork from Martin Fowler, you can realize something like he described
Also check this links maybe the help you