-
Notifications
You must be signed in to change notification settings - Fork 2k
/
PlacementStrategyExtensions.cs
82 lines (76 loc) · 4.06 KB
/
PlacementStrategyExtensions.cs
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
81
82
using System;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Runtime;
using Orleans.Runtime.Placement;
namespace Orleans.Hosting
{
/// <summary>
/// Extensions for configuring grain placement.
/// </summary>
public static class PlacementStrategyExtensions
{
/// <summary>
/// Configures a <typeparamref name="TDirector"/> as the placement director for placement strategy <typeparamref name="TStrategy"/>.
/// </summary>
/// <typeparam name="TStrategy">The placement strategy.</typeparam>
/// <typeparam name="TDirector">The placement director.</typeparam>
/// <param name="builder">The builder.</param>
/// <returns>The builder.</returns>
public static ISiloHostBuilder AddPlacementDirector<TStrategy, TDirector>(this ISiloHostBuilder builder)
where TStrategy : PlacementStrategy
where TDirector : class, IPlacementDirector
{
return builder.ConfigureServices(services => services.AddPlacementDirector<TStrategy, TDirector>());
}
/// <summary>
/// Adds a placement director.
/// </summary>
/// <typeparam name="TStrategy">The placement strategy.</typeparam>
/// <param name="builder">The builder.</param>
/// <param name="createDirector">The delegate used to create the placement director.</param>
/// <returns>The builder.</returns>
public static ISiloHostBuilder AddPlacementDirector<TStrategy>(this ISiloHostBuilder builder, Func<IServiceProvider, IPlacementDirector> createDirector)
where TStrategy : PlacementStrategy
{
return builder.ConfigureServices(services => services.AddPlacementDirector<TStrategy>(createDirector));
}
/// <summary>
/// Configures a <typeparamref name="TDirector"/> as the placement director for placement strategy <typeparamref name="TStrategy"/>.
/// </summary>
/// <typeparam name="TStrategy">The placement strategy.</typeparam>
/// <typeparam name="TDirector">The placement director.</typeparam>
/// <param name="builder">The builder.</param>
/// <returns>The builder.</returns>
public static ISiloBuilder AddPlacementDirector<TStrategy, TDirector>(this ISiloBuilder builder)
where TStrategy : PlacementStrategy
where TDirector : class, IPlacementDirector
{
return builder.ConfigureServices(services => services.AddPlacementDirector<TStrategy, TDirector>());
}
/// <summary>
/// Adds a placement director.
/// </summary>
/// <typeparam name="TStrategy">The placement strategy.</typeparam>
/// <param name="builder">The builder.</param>
/// <param name="createDirector">The delegate used to create the placement director.</param>
/// <returns>The builder.</returns>
public static ISiloBuilder AddPlacementDirector<TStrategy>(this ISiloBuilder builder, Func<IServiceProvider, IPlacementDirector> createDirector)
where TStrategy : PlacementStrategy
{
return builder.ConfigureServices(services => services.AddPlacementDirector<TStrategy>(createDirector));
}
internal static void AddPlacementDirector<TStrategy, TDirector>(this IServiceCollection services)
where TStrategy : PlacementStrategy
where TDirector : class, IPlacementDirector
{
services.AddSingletonNamedService<PlacementStrategy, TStrategy>(typeof(TStrategy).Name);
services.AddSingletonKeyedService<Type, IPlacementDirector, TDirector>(typeof(TStrategy));
}
private static void AddPlacementDirector<TStrategy>(this IServiceCollection services, Func<IServiceProvider, IPlacementDirector> createDirector)
where TStrategy : PlacementStrategy
{
services.AddSingletonNamedService<PlacementStrategy, TStrategy>(typeof(TStrategy).Name);
services.AddSingletonKeyedService<Type, IPlacementDirector>(typeof(TStrategy), (sp, type) => createDirector(sp));
}
}
}