Skip to content

Instantly share code, notes, and snippets.

@tommasobertoni
Created July 19, 2019 21:39
Show Gist options
  • Save tommasobertoni/9b189408d4872a85cfa1b5e5751effdd to your computer and use it in GitHub Desktop.
Save tommasobertoni/9b189408d4872a85cfa1b5e5751effdd to your computer and use it in GitHub Desktop.
Async wrapper
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace App
{
class Program
{
static async Task Main(string[] args)
{
// Instance received from an external API.
var transport = await FetchFromWebAPIAsync();
// Integrate the received data with our values.
await IntegrateDataAsync(transport);
}
private static async Task IntegrateDataAsync(Transport transport)
{
var tasks = new List<Task>();
tasks.Add(Async(async () => transport.Vehichle.Name = await GetVehichleNameAsync(transport.Vehichle.Id)));
if (transport.Stops != null)
{
foreach (var s in transport.Stops)
tasks.Add(Async(async () => s.Description = await GetStopDescriptionAsync(s.Id)));
}
await Task.WhenAll(tasks);
// Local functions
async Task Async(Func<Task> asyncDelegate) =>
await asyncDelegate().ConfigureAwait(false);
}
#region Other methods
private static Task<Transport> FetchFromWebAPIAsync()
{
var t = new Transport
{
Vehichle = new Vehichle { Id = "1234", Name = null },
Stops = new List<Stop>
{
new Stop { Id = "a1", Time = DateTime.Now.AddSeconds(1), Description = null },
new Stop { Id = "b2", Time = DateTime.Now.AddSeconds(5), Description = null },
new Stop { Id = "c3", Time = DateTime.Now.AddSeconds(9), Description = null },
}
};
return Task.FromResult(t);
}
private static Task<string> GetVehichleNameAsync(string vehicleId) => Task.FromResult($"A vehichle: {vehicleId}");
private static Task<string> GetStopDescriptionAsync(string stopId) => Task.FromResult($"A stop: {stopId}");
#endregion
#region Models
class Transport
{
public Vehichle Vehichle { get; set; }
public List<Stop> Stops { get; set; }
}
class Vehichle
{
public string Id { get; set; }
public string Name { get; set; }
}
class Stop
{
public string Id { get; set; }
public DateTime Time { get; set; }
public string Description { get; set; }
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment