可靠集合上的用户-帖子-评论关系实现

Users-Posts-Comments relationship implementation on reliable collections

假设我有三个 类:UserPostComment。标准操作——用户可以创建 posts 并向这些 posts 写评论。所以我们有下面描述的情况:

我需要非常快速地执行这三个查询:

看起来即使我使用分区也不可能。至少没有一本可靠的字典。可能我需要几个字典来处理不同的查询。我说得对吗?

我也在做同样的事情!首先:我的模式有点不同。我有一个用户、一个主题和一条评论。该主题只是一个 class 和一个评论 ID 列表(IEnumerable long),仅此而已。第一条评论是post.

哦,首先,小警告:我刚刚开始使用 Service Fabric,所以我可能做错了 ;)

该用户与我无关。我只是将用户 ID 存储在评论中。在检索评论列表时,我从有状态用户服务中获取用户。或者我直接把用户名存到评论里,还不确定。

所以这给我留下了话题和评论。首先我想到了'lets create a Stateful TopicService and a Stateful CommentService'。但后来我意识到,对于我加载的每个主题,我都需要为每个评论调用 CommentService 以获取评论。

所以我创建了一个 TopicService 来处理 2 个 IReliableDictionaries:主题和评论。

每当评论被 posted 时,我都会使用 TopicId 作为分区键,并且在该分区中存储评论。所以不使用 commentid !这样,针对特定主题的所有评论都在同一个分区中。

当加载包含所有评论的主题时,我再次使用 TopicId 作为分区键,从主题的 reliabledictionary 中获取主题,并在评论的 reliabledictionary 中循环评论 id 列表。不确定是否有帮助,但我的 GetComments 看起来像这样:

        var topics = await this.StateManager.GetOrAddAsync<IReliableDictionary<long, TopicModel>>("topics");
        var comments = await this.StateManager.GetOrAddAsync<IReliableDictionary<long, CommentModel>>("comments");

        List<CommentModel> result = new List<CommentModel>();

        using (var tx = this.StateManager.CreateTransaction())
        {
            ConditionalValue<TopicModel> topic = await topics.TryGetValueAsync(tx, topicid);

            if(topic.HasValue)
            {
                foreach(long commentid in topic.Value.CommentsInternal)
                {
                    ConditionalValue<CommentModel> comment = await comments.TryGetValueAsync(tx, commentid);

                    if (comment.HasValue)
                        result.Add(comment.Value);
                }
            }

            await tx.CommitAsync();
        }

        return result;

我还没有完成,方法还有一些工作要做。

也许这对你有帮助:)

编辑:哎呀,有缺点!当你想通过它的 id 加载单个评论时,你需要提供 topicid。所以我的 CommentModel class 有一个 CommentId 和一个 TopicId 属性.