如何检查特定Id是否已经存在谷物?
考虑到下面的代码将创建一个新的播放器粒,如果Id不存在,它将传递给GetGrain(),我不知道如何检查是否已经存在了。
public async Task<Guid> Create(Guid playerGuid)
{
var player = GrainClient.GrainFactory.GetGrain<IPlayerGrain>(playerGuid);
var gameGuid = await player.CreateGame();
return gameGuid;
}发布于 2017-05-02 21:41:26
简单地说,就是储存一些状态,这样谷物就知道它以前是否被激活了。
在新奥尔良,谷物从来不会被明确地创建或销毁:它们总是可以用来处理请求。因此,从技术上讲,谷物是否存在的概念并不适用于新奥尔良。另一方面,我们可以问“有一粒与这个id曾经被激活过”。
有两种情况您可能需要检查:
IPlayerGrain上调用一个方法,但是播放器不存在。在下面的代码示例中,您可以看到这两种情况:
IPlayerGrain.CreateGame()的调用将引发异常。忽略这样一个事实:我从不设置Created,这可以在某些CreatePlayer(...)方法中完成。IGameGrain.TryCreateGame(player)的调用将返回false。在这种情况下,IPlayerGrain.CreateGame()将继续循环,直到找到一个尚未创建的游戏。有了Guid id,你就不太可能看到碰撞,但我理解谨慎的愿望--以防星星对着你。public interface IPlayerGrain : IGrainWithGuidKey
{
Task<Guid> CreateGame();
}
public class PlayerState
{
public bool Created { get; set; }
}
public class PlayerGrain : Grain<PlayerState>, IPlayerGrain
{
public async Task<Guid> CreateGame()
{
if (!this.State.Created)
throw new InvalidOperationException("Player does not exist.");
var thisPlayer = this.AsReference<IPlayerGrain>();
var created = false;
var gameId = default(Guid);
while (!created)
{
// Get a new, random game grain
gameId = Guid.NewGuid();
// Try to create a game.
created = await this.GrainFactory.GetGrain<IGameGrain>(gameId)
.TryCreateGame(thisPlayer);
// If the game was successfully created, break out and return the id.
// Otherwise, keep looping.
}
return gameId;
}
}public interface IGameGrain : IGrainWithGuidKey
{
// Returns true if game was created, false otherwise.
Task<bool> TryCreateGame(IPlayerGrain player);
}
public class GameState
{
public IPlayerGrain Player { get; set; }
}
public class GameGrain : Grain<GameState>, IGameGrain
{
public async Task<bool> TryCreateGame(IPlayerGrain player)
{
// If this grain already has a player, return false.
if (this.State.Player != null) return false;
// Otherwise, set the player, write it to storage, and return true.
this.State.Player = player;
await this.WriteStateAsync();
return true;
}
}您问题中的Create方法不需要更改。
https://stackoverflow.com/questions/43745031
复制相似问题