0

10秒ごとに3つのhttpリクエストをWebサービスに送信します。応答は、キャッシュクラス内の3つのメソッド(httpクエリ/リクエストごとに1つ)に渡され、前回から応答の内容が変更されたかどうかを確認します。

生の応答コンテンツを文字列に変換し、キャッシュクラスにプライベート文字列として保存されている古い応答と比較します。正常に機能しますが、次のように、このアプローチには重複したコードがたくさんあります。

    class Cache
{
    private HubClient _hubClient;
    private string oldIncidentAppointment;
    private string oldIncidentGeneral;
    private string oldIncidentUntreated;

    public Cache(HubClient hubClient)
    {
        _hubClient = hubClient;
    }

    public bool IsIncidentAppointmentNew(string currentIncidentAppointment)
    {
        if (XElement.Equals(oldIncidentAppointment, currentIncidentAppointment))
        {
            return false;
        }
        else
        {
            oldIncidentAppointment = currentIncidentAppointment;
            _hubClient.SendToHub();
            return true;
        }
    }

    public bool IsIncidentUntreatedNew(string currentIncidentUntreated)
    {
        if (XElement.Equals(oldIncidentUntreated, currentIncidentUntreated))
        {
            return false;
        }
        else
        {
            oldIncidentUntreated = currentIncidentUntreated;
            _hubClient.SendToHub();
            return true;
        }
    }

    public bool IsIncidentGeneralNew(string currentIncidentGeneral)
    {
        if (XElement.Equals(oldIncidentGeneral, currentIncidentGeneral))
        {
            return false;
        }
        else
        {
            oldIncidentGeneral = currentIncidentGeneral;
            _hubClient.SendToHub();
            return true;
        }
    }
}

これを、現在および将来のすべてのhttpクエリメソッドの新旧のコンテンツを比較する一般化されたメソッドにリファクタリングするにはどうすればよいですか?

4

2 に答える 2

1

これは簡単で汚いので、100% でな​​い場合は修正する必要があります。それが正しいことを確認するためのテストはありません。また、存在を確認せずに存在しないキーを辞書に問い合わせることができるかどうかもわからないので、それを処理する必要があるかもしれません。

class Cache
{
    private HubClient _hubClient;
    private IDictionary<string, string> _oldIncidents;

    public Cache(HubClient hubClient)
    {
        _hubClient = hubClient;
        _oldIncidents = new Dictionary<string, string>();
    }

    public bool IsIncidentAppointmentNew(string currentIncidentAppointment)
    {
        return DoMagicWork(
            incidentKey: "appointment",
            currentIncident = currentIncidentAppointment
        );
    }

    public bool IsIncidentUntreatedNew(string currentIncidentUntreated)
    {
        return DoMagicWork(
            incidentKey: "untreated",
            currentIncident = currentIncidentUntreated
        );
    }

    public bool IsIncidentGeneralNew(string currentIncidentGeneral)
    {
        return DoMagicWork(
            incidentKey: "general",
            currentIncident = currentIncidentGeneral
        );
    }

    private bool DoMagicWork(string incidentKey, string currentIncident)
    {
        var oldIncident = _oldIncidents[incidentKey];
        if (XElement.Equals(oldIncident, currentIncident))
        {
            return false;
        }

        _oldIncidents[incidentKey] = currentIncident;
        _hubClient.SendToHub();
        return true;
    }
}
于 2012-11-26T16:57:09.207 に答える