交易C#的对象? [英] Transactions for C# objects?

查看:146
本文介绍了交易C#的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是好奇,是有在普通C#对象交易的支持?像

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

using (var transaction = new ObjectTransaction(obj))
{
  try
  {
    obj.Prop1 = value;
    obj.Prop2 = value;
    obj.Recalculate(); // may fire exception
    transaction.Commit(); // now obj is saved
  }
  except
  {
     transaction.Rollback(); // now obj properties are restored
  }
}

只是为了让答案更加有用;-)还有什么其他语言的相似?

Just to make answers more useful ;-) is there anything similar in other languages?

在STM更新:这里就是它声称:

Update on STM: here's what it claims:

atomic {
  x++;
  y--;
  throw;
}

将离开X / Y不变,包括链式方法的调用。貌似我问。至少这是非常有趣的。我认为这是足够接近。此外,还有是其他语言类似的事情,例如哈斯克尔STM。请注意,我不说,它应该用于生产; - )

will leave x/y unchanged, including chained methods calls. Looks like what I ask for. At least it's very interesting. I think that's close enough. Also, there're similar things in other languages, for example Haskell STM. Notice I don't say that it should be used for production ;-)

推荐答案

微软正在努力就可以了。阅读关于软件事务内存。

Microsoft is working on it. Read about Software Transactional Memory.

  • STM.NET
  • STM.NET Team Blog
  • Channel 9 Video: STM.NET: Who. What. Why.
  • Papers on STM

他们使用了一些不同的语法:

They use a few different syntaxes:

// For those who like arrows
Atomic.Do(() => { 
    obj.Prop1 = value;
    obj.Prop2 = value;
    obj.Recalculate();
});

// For others who prefer exceptions
try { 
    obj.Prop1 = value;
    obj.Prop2 = value;
    obj.Recalculate();
}
catch (AtomicMarker) {
}

// we may get this in C#:
atomic { 
    obj.Prop1 = value;
    obj.Prop2 = value;
    obj.Recalculate();
}

这篇关于交易C#的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆