-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
80 lines (74 loc) · 3.94 KB
/
Copy pathProgram.cs
File metadata and controls
80 lines (74 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using AutoMapper;
using System.ComponentModel;
using System.Text.Json;
using System;
using System.Threading.Tasks;
using AsyncMapper;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http;
using System.Collections.Generic;
using System.Linq;
namespace AsyncMapper.Examples {
public partial class Program
{
static async System.Threading.Tasks.Task Main()
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton<HttpClient>();
services.AddTransient<ISomeService>(sp => new SomeService((i) => i * 2));
services.AddAsyncMapper(typeof(ProfileMarkerType));
services.AddHttpClient("Api", httpClient => {
httpClient.BaseAddress = new Uri("http://127.0.0.1:5000");
});
services.AddHttpClient("FastApi", httpClient => {
httpClient.BaseAddress = new Uri("http://127.0.0.1:5000");
httpClient.DefaultRequestHeaders.Add("no-delay", "");
});
var provider = services.BuildServiceProvider();
using (var scope = provider.CreateScope())
{
// fetch all data from server with no added delay
var httpFactory = scope.ServiceProvider.GetService<IHttpClientFactory>();
var fastClient = httpFactory.CreateClient("FastApi");
var people = JsonSerializer.Deserialize<List<Person>>(await fastClient.GetStringAsync("/person?id=all"));
var tasks = JsonSerializer.Deserialize<List<PartialTask>>(await fastClient.GetStringAsync("/task?id=all"));
var mapper = scope.ServiceProvider.GetService<IAsyncMapper>();
// prints when each mapping starts and ends
int c;
async Task<TDestination> MapWithLogging<TSource, TDestination>(TSource p)
{
int index = c++;
Console.WriteLine($"Start mapping {typeof(TSource).Name} {index} into {typeof(TDestination).Name} at {DateTime.Now}");
var ret = await mapper.Map<TDestination>(p, MappingOptions.MapSequentially);
Console.WriteLine($"End mapping {typeof(TSource).Name} {index} into {typeof(TDestination).Name} at {DateTime.Now}");
return ret;
}
// map Person to Worker
// runs all maps in parallel
// each map calls 2 resolvers
c = 0;
Console.WriteLine($"Start mapping people at {DateTime.Now}");
var workers = await mapper.Map<Worker>(people, MappingOptions.MapInParallel);
Console.WriteLine($"End mapping people at {DateTime.Now}");
// map Worker back to Person
// runs instantly because no resolvers are needed
Console.WriteLine($"Start mapping workers at {DateTime.Now}");
Person[] _ = await System.Threading.Tasks.Task.WhenAll(people.Select(p => mapper.Map<Person>(p)));
Console.WriteLine($"End mapping workers at {DateTime.Now}");
// map PartialTask to Task
// runs all maps in parallel
// each map calls 2 resolvers
c = 0;
Console.WriteLine($"Start mapping tasks at {DateTime.Now}");
Task[] fullTasks = await System.Threading.Tasks.Task.WhenAll(tasks.Select(t => MapWithLogging<PartialTask, Task>(t)));
Console.WriteLine($"End mapping tasks at {DateTime.Now}");
// map Task back to PartialTask
// runs instantly because no resolvers are needed
Console.WriteLine($"Start mapping tasks at {DateTime.Now}");
await System.Threading.Tasks.Task.WhenAll(fullTasks.Select(t => mapper.Map<PartialTask>(t)));
Console.WriteLine($"End mapping workers at {DateTime.Now}");
}
}
}
}