Should I violate S in SOLID or should I violate the DRY principle?

I had a way to send GET requests, for example:

private JArray GetRESTData(string uri)
{
    try
    {
        var webRequest = (HttpWebRequest)WebRequest.Create(uri);
        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        var reader = new StreamReader(webResponse.GetResponseStream());
        string s = reader.ReadToEnd();
        return JsonConvert.DeserializeObject<JArray>(s);
    }
    catch // This method crashes if only one json "record" is found - try this:
    {
        try
        {
            MessageBox.Show(GetScalarVal(uri));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    return null;
}

... I changed it to handle POST requests by inserting this between the webRequest and webResponse assignments:

if (uri.ToUpper().Contains("POST"))
{
    webRequest.Method = "POST";
    webRequest.ContentLength = 0;
}

... and renamed it GetOrPostRESTData ()

But this violates the principle of shared responsibility.

However, if I do this in two ways, the POST method is similar to the GET method, except for the additional two lines of code that are otherwise in the conditional ("if Post"), I violate DRY, since most of the code is the same.

Is there a third way? The middle way? Or should I choose between these two violations? I am stuck between a dry and hard place.

+4
1

, ? , , GET POST , - ProcessRequest. , SRP - - , , , URI, - .

+9

Source: https://habr.com/ru/post/1525479/


All Articles