Would it be correct to use a static class to consume a web service?

1

A question that comes up when I'm programming when it comes to programming static classes.

Microsoft's official documentation says the following about static classes:

  

The static class can be used as a convenient container for sets of   methods that just operate on input parameters and do not have to get   or set any internal instance fields.

So here it says static classes can be used as a container for methods that only operate on input parameters and do not need to work with instance fields.

Let's now turn to a more specific problem. My ASP.NET application consumes a web service that will return me business-related information. To consume the web service I must do the authentication along with it, and after that send HTTP requests, simple, so I created a class, which I will call here ApiClient .

At no point in the process do I need to access instance resources of the ApiClient class, taking into account that data such as the endpoint URL, access key, and validity period of the access key will be the same for all users who consume the methods of class ApiClient .

So far I imagine the choice in user a static class is correct. My question is the following, in case of multiple simultaneous calls of a method of class ApiClient , if a request is delayed say 5 seconds, these requests would be queued, causing the static class to become a bottleneck in the consumption of the web service ?

Below is an example of the ApiClient class I tried to describe.

public static class ApiClient
{
    private static string baseUri = "http://baseuri.com";

    private static async Task<string> GetKey()
    {
        //Get the key
    }

    private static async Task<string> SendRequest(string resource)
    {
        string key = await GetKey();

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("key", key);

        string url = $"{baseUri}{resource}";
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
        HttpResponseMessage response = await client.SendAsync(request);

        string result = await response.Content.ReadAsStringAsync();
        return result;
    }

    public static async Task<CustomerInfo> GetCustomerInfo(string customerId)
    {
        string resource = $"/api/method/{customerId}";
        string json = await SendRequest(resource);
        CustomerInfo customerInfo = JsonConvert.DeserializeObject<CustomerInfo>(json);
        return customerInfo;
    }       
}

Could this approach be considered problematic? In that case would I have a bottleneck problem if multiple requests are passed to the SendRequest method? If yes, could this problem be solved by changing the approach and instantiation of the ApiClient class?

    
asked by anonymous 05.03.2017 / 06:06

1 answer

1

First, in fact I consider a correct approach. If it meets your needs I can not say. Who defends OOP already say that can not do this, who is pragmatic at the beginning does not see problems. Anyone who thinks that no code can be written if they do not have tests will say that they can not be tested, who is pragmatic knows that it only imposes a different technique if formal tests are actually being done.

There is no difference in running static or instance methods. The only difference between them is that the instance method receives an extra hidden parameter called this . There is no different method for each instance, there is only one for the whole application, so its simultaneous execution is the same as the static method. The instance has been individual, but the behavior always belongs to the class, even if it gives the impression of being different.

You may have problems with SendRequest() if it contains a competition problem, which you can not evaluate with what was put, it is not possible to create a bottleneck, you could access two resources at the same time can be accessed at the same time, so it would require coordination. But this is hypothetical, nothing indicates that this is the case.

I would only baseUri readonly and eliminate unnecessary variables.

    
05.03.2017 / 11:11