信頼できるコレクションの使用についてはthisの記事を読んでください。信頼できるコレクションにオブジェクトを変更してはいけません。値のコピー(クローン)を取得し、クローンされた値をチェクニングして、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();
}
}
私Quesionは次のとおりです。私は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();
}
}
ありがとう!