如何重构以下代码

How to refactor the following code

嗨,下面的 BackendHubProxy 继承了 Hub,我还有另一个 class 使用 GlobalHost.ConnectionManager.GetHubContext();

下面重构什么更好?

    private static void OnGamesDisabled(Guid playerId)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<BackendHubProxy>();
        hubContext.Clients.User(playerId.ToString("N")).DisablePlayer();
    }

    private static void OnGamesDeauthorized(Guid playerId)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<BackendHubProxy>();
        hubContext.Clients.User(playerId.ToString("N")).DeauthorizePlayer();
    }

    private static void OnGamesChanged(string[] lobbyClientIds)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<FrontendHubProxy>();
        hubContext.Clients.Groups(lobbyClientIds).GamesChanged();
    }

您没有明确说明您的最终目标是什么,但假设您正在尝试消除重复,可以将重复提取到一种方法。

private static Clients getClients()
{
    var hubContext = GlobalHost.ConnectionManager.GetHubContext<BackendHubProxy>();
    return hubContext.Clients;
}

private static void OnGamesDisabled(Guid playerId)
{
    getClients().User(playerId.ToString("N")).DisablePlayer();
}

private static void OnGamesDeauthorized(Guid playerId)
{
    getClients().User(playerId.ToString("N")).DeauthorizePlayer();
}

private static void OnGamesChanged(string[] lobbyClientIds)
{
    getClients().Groups(lobbyClientIds).GamesChanged();
}