首页 > 解决方案 > 有效地从 Orleans Grain 获取状态

问题描述

我在奥尔良为比赛的球员准备了一粒粮食。玩家有几个我想在客户端直接访问的属性。是否有可能,是否有效,将这些作为公共财产是否有意义?或者,我是否应该有一个 GetAllState 方法,该方法返回一个 DTO,其中包含谷物中这些属性的当前值?

public interface IPlayerGrain : IGrainWithIntegerKey
{
    // Individual public properties to access grain state?
    string Name { get; }
    int Score { get; }
    int Health { get; }
 
    // Or, get all the current grain state as DTO?
    Task<PlayerState> GetAllState(); 
}

根据我目前的理解,我认为我将需要使用 GetAllState,因为我认为任何与谷物的通信都需要通过一种方法,这可能会在孤岛之间传递。因此,您可能希望最小化传递的消息数量,并且不想传递三个消息来获取名称、分数和健康。或者,消息传递是否很便宜,而不是我应该担心做太多事情?在我的示例中,我只包含了 3 个属性,但在我的真实游戏中还会有更多。

但是,我真的不喜欢拥有一个只是谷物内部属性副本的贫血 DTO 模型的想法。

所以我想知道是否有更好的方法,或者在奥尔良这种事情的首选模式?

标签: orleans

解决方案


Introduction of a complex return type to minimize roundtrips is a valid solution.

However, I wouldn't return the whole internal state, as I assume that not all the clients need all the data all the time. It may also be a sign that you have a business logic implemented outside of the grains and you should move it into the grain.


推荐阅读