比较来自不同来源的新旧内容的通用方法

时间:2012-11-26 16:43:01

标签: c# refactoring

我每隔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查询方法的新旧内容?

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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 ) ) {

}