In .NET there is such a wonderful thing as TransactionScope .

By marking a domain using all the work with the database in a certain domain will be in an implicit transaction.

Does there exist (if not, is it realizable) something similar for ordinary variables?

Let's say that a set of objects is passed to the method and it is important that “all or nothing” be executed in a certain area. If nothing happens, the calling code will not see any changes and seeing the error will be able to try to re-do the method without worrying that the data is in an inconsistent state.

For example:

  public bool TryDoWork(Someclass someclass) { using (var tran=new TransactionScope()) { try { someclass.Field = 100500; someclass.Modify("123qwe"); } catch (Exception e) { return false; } tran.Commit(); } } 

Some abstract class is passed. Accordingly, if Commit is not called, the class variable is not in an inconsistent state and the external code does not need to worry about correcting the data before attempting to do something again.

This article says something similar, but is it possible to create a universal solution taking into account the nature of classes?

PS Question of interest for the sake of.

  • add example code explaining by example - Grundy
  • @Grundy added. - iluxa1810
  • I also asked such a question. Rollbacks and commits are implemented inside databases. And in simple classes there is nothing like that. You need to implement yourself (or look for the finished one). - Alexander Petrov
  • Software transactional memory - there are implementations in C #. But I did not try it; I can’t say anything about quality. UPDATE: it turns out in the article on HabrĂ© there is a link to a wiki (Russian) with a similar article. But in the English version of the list of libraries more. - Alexander Petrov

0