2
\$\begingroup\$

I need advice for this implementation that I made for a service access portable class library referenced to Xamarin.Android and Xamarin.Ios projects. For now, it works fine, but I have doubts about its performance or if it would crash on simultaneous calls.

public class HttpClientBase : HttpClient
{

    private static readonly HttpClientBase instance = new HttpClientBase();

    static HttpClientBase() { }

    private HttpClientBase() : base() 
    {
        Timeout = TimeSpan.FromMilliseconds(15000);
        MaxResponseContentBufferSize = 256000;
    }

    public static HttpClientBase Instance
    {
        get
        {
            return instance;
        }
    }

    public async Task<List<T>> GetListItems<T>(string Url)
    {
        var uri = new Uri(string.Format(Constantes.BaseUrlApi, Url));
        var response = await GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            var Items = JsonConvert.DeserializeObject<List<T>>(content);
            return Items;
        }
        throw new Exception(response.ReasonPhrase);
    }

    public async Task<T> GetItem<T>(string Url)
    {
        var uri = new Uri(string.Format(Constantes.BaseUrlApi, Url));
        var response = await GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            var Item = JsonConvert.DeserializeObject<T>(content);
            return Item;
        }
        throw new Exception(response.ReasonPhrase);
    }

    public async Task PostItem<T>(T item, string url)
    {
        var uri = new Uri(string.Format(Constantes.BaseUrlApi, ""));
        var json = JsonConvert.SerializeObject(item);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        HttpResponseMessage response = null;
        response = await PostAsync(uri, content);
        if (response.IsSuccessStatusCode)
        {
            return;
        }
        throw new Exception(response.ReasonPhrase);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Personally i don't see issues with simultaneous calls \$\endgroup\$ Dec 14, 2016 at 9:53
  • 1
    \$\begingroup\$ Only nitpicking I could add is the 2 magic numbers in constructor might be a tiny bit clearer as constants at the top of the class. \$\endgroup\$
    – Shelby115
    Dec 14, 2016 at 16:31

1 Answer 1

4
\$\begingroup\$

General remarks

The naming and the design are ok. The only thing that could be improved is the

 HttpClientBase

It's not a base class so you shouldn't call it like this. I suggest HttpClientService.


Timeout = TimeSpan.FromMilliseconds(15000);

Or simply TimeSpan.FromSeconds(15);


Singleton

There are of course mutliple patters for creating a singleton but the one you chose is probably the right one Singleton by Jon Skeet clarification


Concurrency

I have doubts about its performance or if it would crash on simultaneous calls.

I don't see actually anything that could be further optimized. You call only core APIs that should already do their best.

The methods don't share any data with each other so there is no risk in calling them in parallel.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.