0

我每 10 秒向 Web 服务发送三个 http 请求。响应被传递给缓存类中的三个方法(每个 http 查询/请求一个),用于检查响应内容自上次以来是否发生了变化。

我将原始响应内容转换为字符串并将其与旧响应进行比较,旧响应存储为缓存类中的私有字符串。它工作正常,但该方法有很多重复的代码,如您所见:

    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 回答
1

您可以将它们存储在字典中:

class Cache {

    private HubClient _hubClient;
    private Dictionary<string, string> _pages;


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

    public bool isPageNew( string key, string content ) {
        string current;
        if (_pages.TryGetValue(key, out current) && XElement.Equals(current, content)) {
            return false;
        }

        _pages[key] = content;
        _hubClient.SendToHub(); //Why have side effect here? :P
        return true;
    }
}

然后:

Cache cache = new Cache( client );

if( cache.isPageNew( "untreated", pageContent ) ) {

}
于 2012-11-26T17:05:14.780 回答