Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active August 24, 2017 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/007329b14dc07739fba2df39e3b43fef to your computer and use it in GitHub Desktop.
Save dcomartin/007329b14dc07739fba2df39e3b43fef to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace LazyAsync
{
public class LazyAsync<T> : Lazy<Task<T>>
{
public LazyAsync(Func<Task<T>> taskFactory) : base(
() => Task.Factory.StartNew(taskFactory).Unwrap()) { }
}
class Program
{
public static async Task Main(string[] args)
{
var currencyService = new CurrencyService();
var rates = await currencyService.ExchangeRates.Value;
var cad = rates.Rates.Single(x => x.Key == "CAD");
Console.WriteLine($"USD to CAD = {cad.Value}");
Console.ReadKey();
}
}
public class CurrencyService : IDisposable
{
private readonly HttpClient _httpClient;
public LazyAsync<ExchangeRates> ExchangeRates { get; set; }
public CurrencyService()
{
_httpClient = new HttpClient();
ExchangeRates = new LazyAsync<ExchangeRates>(async () =>
{
var json = await _httpClient.GetStringAsync("http://api.fixer.io/latest?base=USD");
return JsonConvert.DeserializeObject<ExchangeRates>(json);
});
}
public void Dispose()
{
_httpClient?.Dispose();
}
}
public class ExchangeRates
{
public string Base { get; set; }
public DateTime Date { get; set; }
public Dictionary<string, decimal> Rates { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment