【发布时间】:2017-05-11 22:51:03
【问题描述】:
我阅读了 this 关于使用 Reliable Collections 的文章,其中提到 一旦将对象提供给可靠的集合,就不得修改它,以及更新中值的正确方法一个可靠的集合,就是获取一个值的副本(克隆),检查克隆的值,然后更新RC中的克隆值。
使用不当:
using (ITransaction tx = StateManager.CreateTransaction()) {
// Use the user’s name to look up their data
ConditionalValue<User> user =
await m_dic.TryGetValueAsync(tx, name);
// The user exists in the dictionary, update one of their properties.
if (user.HasValue) {
// The line below updates the property’s value in memory only; the
// new value is NOT serialized, logged, & sent to secondary replicas.
user.Value.LastLogin = DateTime.UtcNow; // Corruption!
await tx.CommitAsync();
}
}
我的问题是:为什么我将对象交给 RC 后就不能修改它?为什么我必须在更改对象之前克隆对象?为什么我不能做类似的事情(在同一个事务中更新对象):
using (ITransaction tx = StateManager.CreateTransaction()) {
// Use the user’s name to look up their data
ConditionalValue<User> user =
await m_dic.TryGetValueAsync(tx, name);
// The user exists in the dictionary, update one of their properties.
if (user.HasValue) {
// The line below updates the property’s value in memory only; the
// new value is NOT serialized, logged, & sent to secondary replicas.
user.Value.LastLogin = DateTime.UtcNow;
// Update
await m_dic.SetValue(tx, name, user.Value);
await tx.CommitAsync();
}
}
谢谢!
【问题讨论】:
标签: c# .net azure azure-service-fabric