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

C# 比较不同来源的新旧内容的通用方法,c#,refactoring,C#,Refactoring,我每10秒钟向web服务发送三个http请求。响应被传递给缓存类中的三个方法(每个http查询/请求一个),这些方法检查自上次以来响应内容是否发生了更改 我将原始响应内容转换为字符串,并将其与旧响应进行比较,旧响应作为私有字符串存储在缓存类中。它工作正常,但该方法有大量重复代码,如您所见: class Cache { private HubClient _hubClient; private string oldIncidentAppointment; priva

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

这是一个快速而肮脏的方法,因此如果它不是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;
    }
}
类缓存
{
私人HubClient(HubClient);;
私人词典;
公共缓存(HubClient HubClient)
{
_hubClient=hubClient;
_OldEvents=新字典();
}
公共bool IsIncidentAppointNew(字符串currentIncidentAppointment)
{
返回域magicWork(
附带说明:“任命”,
currentIncident=currentIncident约会
);
}
公共布尔值IsIncidentUntreatedNew(字符串currentIncidentUntreated)
{
返回域magicWork(
incidentKey:“未处理”,
currentIncident=currentIncident未经处理
);
}
公共bool IsIncidentGeneralNew(字符串currentIncidentGeneral)
{
返回域magicWork(
附带说明:“将军”,
currentIncident=currentIncidentGeneral
);
}
私有bool-DoMagicWork(string incidentKey,string currentIncident)
{
var oldIncident=_oldIncidents[incidentKey];
if(XElement.Equals(oldIncident,currentIncident))
{
返回false;
}
_oldIncidents[incidentKey]=当前事件;
_hubClient.SendToHub();
返回true;
}
}

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

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;
    }
}

是的,我也这么想。但这种方法需要我在字典中硬编码值,因此扩展性不强。我还有其他选择吗?Func?你在字典里硬编码是什么意思?字典会将任意数量的任意字符串(当然是在内存限制内)映射到任意网页内容。我明白你的意思,我的想法是错误的。谢谢,这似乎是一个很好的解决方案。您认为使用Func或类似工具可以实现同样的效果吗?我问这个问题主要是出于好奇。你可以做我想象的任何事情,但是因为它的功能都是一样的,所以我觉得它没有多大用处。如果我使用一个函数,那是因为不同的缓存项需要不同的缓存策略或其他一些自定义处理,但所有这些都是相同的。谢谢你的回答。你说的“副作用”是什么意思?@user1750323好吧,这个函数表明它会给你一个肯定或否定的答案。但是它也有一个副作用(做一些IO?),这对调用者来说并不明显。
Cache cache = new Cache( client );

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

}