Transactions for C# objects?

前端 未结 8 766
青春惊慌失措
青春惊慌失措 2020-12-01 09:26

Just curious, is there any support for transactions on plain C# objects? Like

using (var transaction = new ObjectTransaction(obj))
{
  try
  {
    obj.Prop1          


        
相关标签:
8条回答
  • 2020-12-01 10:18

    And here again simple solution is not to allow your objects to get into invalid state in the first place. Then you don't need to roll anything back, you don't need to call "Validate" etc. If you remove your setters and start thinking about sending messages to objects to do something on internal data, rather then setting properties, you'll uncover subtle things about your domain, that otherwise you would not.

    0 讨论(0)
  • 2020-12-01 10:19

    You can make a copy of the object prior to executing methods and setting properties. Then, if you don't like the result, you can just "roll back" to the copy. Assuming, of course, that you have no side-effects to contend with.

    0 讨论(0)
提交回复
热议问题