-1

I have following code:

File.Copy(pathSource, pathDestination);   //copy file
ID3v2TagToDB(pathSource, pathDestination); //read his tags and save them to DB
ID3v2TagToTXT(pathSource, pathDestination); //read his tags and save them to txt
dgv.Rows[chosen[i]].DefaultCellStyle.BackColor = Color.GreenYellow; //color green datagridview row if copy and read successfull

I want to ensure that each operation is successfull. If any of them failed, I need to rollback all previous ones.

And also, warn user about error that ocurred, and operation that ocurred in.

leroy
  • 63
  • 1
  • 8
  • For similar purposes you can use (I suppose) exception mechanism. (Maybe there is another way in .net) – Alex Aparin Apr 02 '17 at 15:15
  • Isn't it a good candidate for a memento pattern? – FCin Apr 02 '17 at 15:17
  • @FCin memento is more related to abstract the state persistance of the object. In this case the question is focused on the transactional operation itfself. – Mr Rivero Apr 02 '17 at 15:24

2 Answers2

0

There is no built in way to do this. you will need to think about what your application does and provide ways of undoing each operation.

For instance, you can delete the file you copied, you can delete the rows you inserted, and likewise remove the text you wrote to file.

Look into exception handling

Kir
  • 2,905
  • 2
  • 27
  • 44
0

I suggest you to implement a .NET Two Phase Commit Resource Manager. You RM could save a backup file for each change you made it to the filesystem, when the DTC components invokes the logic to rollback your changes. You could restore the backups files you made previously.

Check out this documentation

Using the TxF and KTM would help you as described here and here, because is already implemented in Vista+ windows versions.

TxF (Transactional Filesystem) works with NTFS format. The Win32 API contract wasn't changed, this is a great option for you. Because all the API of IO, exposed throught .NET Framework are the same. You only need to call a few Win32 API directly and you got all the benefits of this new feature included in Windows. Using the DTC will allows you to do the transactional work across filesystem and database too. So In case of error throw, all the changes are rollback across the database and the filesystem.

Mr Rivero
  • 1,248
  • 7
  • 17