I'm a little confused how to implement Dispose pattern correctly.
I have a class:
public class DomainActions : IDomainActions, IDisposable
{
private HttpClient _client;
public DomainActions(string baseAddress, string token)
{
_client = new HttpClient();
_client.BaseAddress = new Uri(baseAddress);
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
public async Task<List<DomainDto>> GetDomainListAsync()
{
var requestStream = await _client.GetAsync("domains");
var requestString = await requestStream.Content.ReadAsStringAsync();
return ParseDomainListResponse(requestString);
}
as I understand, I have to allocate _client
public class DomainActions : IDomainActions
{
//...
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool _disposed = false;
public virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
if (_client != null)
{
_client.Dispose();
_client = null;
}
}
_disposed = true;
}
}
my implemetation is correct, is it enough?