【问题标题】:Generalized method that compares old and new content from different sources比较不同来源的新旧内容的通用方法
【发布时间】:2012-11-26 16:43:01
【问题描述】:

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

【问题讨论】:

  • 是的,我也在想同样的事情。但是这种方法需要我对字典中的值进行硬编码,因此扩展性不是很好。我还有什么其他选择?功能?
  • 字典中的硬编码是什么意思?字典会将任意数量(当然在内存限制内)的任意字符串映射到任意网页内容。
  • 我明白你的意思,我想错了。

标签: c# refactoring


【解决方案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 ) ) {

}

【讨论】:

  • 感谢您的回答。 “副作用”是什么意思?
  • @user1750323 这个函数表明它会给你一个是或否的答案。但它也有一个副作用(做一些 IO?),这对调用者来说并不明显。
【解决方案2】:

这又快又脏,所以如果不是 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;
    }
}

【讨论】:

  • 谢谢,这似乎是一个很好的解决方案。你认为使用 Func 或类似的东西可以用来实现同样的事情吗?我主要是出于好奇。
  • 你可以做任何我想象的事情,但因为它的功能都是一样的,我看不出它有多大用处。如果我要使用一个函数,那是因为不同的缓存项需要不同的缓存策略或其他一些自定义处理,但所有这些都是相同的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 2011-08-18
  • 2011-06-07
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多