服务结构参与者状态和列表 [英] Service fabric actor state and list

查看:122
本文介绍了服务结构参与者状态和列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否建议在Service fabric actor中有一个列表?我试图将用户收藏夹保留在用户Actor中.在这种情况下最好的方法是什么?

Is it recommended to have a list in Service fabric actor? I am trying to keep User favorites in a User Actor. What is best approach for this scenario?

推荐答案

是的,只要您将列表视为不可变的.

Yes, as long as you treat the list as immutable.

状态管理器的检索方法返回对以下对象的引用: 本地内存.仅在本地内存中修改此对象不会 使它被持久保存.当从 状态管理器并进行修改,必须将其重新插入状态 经理被持久保存.

The state manager retrieval methods return a reference to an object in local memory. Modifying this object in local memory alone does not cause it to be saved durably. When an object is retrieved from the state manager and modified, it must be reinserted into the state manager to be saved durably.

-

下面的UserInfo类型演示了如何定义不可变类型 利用上述建议.

The UserInfo type below demonstrates how to define an immutable type taking advantage of aforementioned recommendations.

[DataContract]
// If you don’t seal, you must ensure that any derived classes are also immutable
public sealed class UserInfo {
   private static readonly IEnumerable<ItemId> NoBids = ImmutableList<ItemId>.Empty;

   public UserInfo(String email, IEnumerable<ItemId> itemsBidding = null) {
      Email = email;
      ItemsBidding = (itemsBidding == null) ? NoBids : itemsBidding.ToImmutableList();
   }

   [OnDeserialized]
   private void OnDeserialized(StreamingContext context) {
      // Convert the deserialized collection to an immutable collection
      ItemsBidding = ItemsBidding.ToImmutableList();
   }

   [DataMember]
   public readonly String Email;

   // Ideally, this would be a readonly field but it can't be because OnDeserialized
   // has to set it. So instead, the getter is public and the setter is private.
   [DataMember]
   public IEnumerable<ItemId> ItemsBidding { get; private set; }

   // Since each UserInfo object is immutable, we add a new ItemId to the ItemsBidding
   // collection by creating a new immutable UserInfo object with the added ItemId.
   public UserInfo AddItemBidding(ItemId itemId) {
      return new UserInfo(Email, ((ImmutableList<ItemId>)ItemsBidding).Add(itemId));
   }
}

更多信息: 1 2

这篇关于服务结构参与者状态和列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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