如何检查某个ID是否已存在?
考虑到下面的内容会创建一个新的播放器粒度,如果它不存在,我会传入GetGrain()
,我不知道如何检查是否已经存在。
public async Task<Guid> Create(Guid playerGuid)
{
var player = GrainClient.GrainFactory.GetGrain<IPlayerGrain>(playerGuid);
var gameGuid = await player.CreateGame();
return gameGuid;
}
答案 0 :(得分:7)
简短的回答是存储一些状态,以便谷物知道它是否先前已被激活。
奥尔良的谷物永远不会被明确地创建或销毁:它们始终可用于处理请求。因此,从技术上讲,谷物是否存在的概念并不适用于奥尔良。另一方面,我们可以在&#34;之前询问&#34;是否已经激活的谷物。
您可能需要检查两种情况:
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
方法无需更改。