-1
建议在服务结构演员中有一个列表吗?我正在尝试将用户收藏夹保存在用户演员中。这种情况下最好的方法是什么?服务结构演员状态和列表
建议在服务结构演员中有一个列表吗?我正在尝试将用户收藏夹保存在用户演员中。这种情况下最好的方法是什么?服务结构演员状态和列表
是的,只要您将列表视为不可变的。
状态管理器检索方法返回对本地内存中对象的引用。仅在本地存储器中修改此对象不会导致它被长期保存,因此不会被 保存。从 状态管理器中检索并修改对象时,必须将其重新插入状态为 的管理器,以便持久保存。
-
以下用户信息类型演示了如何定义一个不可变型 采取的上述建议的优势。
[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));
}
}