-
Notifications
You must be signed in to change notification settings - Fork 2k
/
KeyedService.cs
167 lines (145 loc) · 6.29 KB
/
KeyedService.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
namespace Orleans.Runtime
{
public class KeyedService<TKey, TService> : IKeyedService<TKey, TService>
where TService : class
{
private readonly Func<IServiceProvider, TKey, TService> factory;
public TKey Key { get; }
public KeyedService(TKey key, IServiceProvider services, Func<IServiceProvider, TKey, TService> factory)
{
this.Key = key;
this.factory = factory;
}
public TService GetService(IServiceProvider services) => factory(services,Key);
public bool Equals(TKey other)
{
return Equals(Key, other);
}
}
public class KeyedService<TKey, TService, TInstance> : KeyedService<TKey, TService>
where TInstance : TService
where TService : class
{
public KeyedService(TKey key, IServiceProvider services)
: base(key, services, (sp, k) => sp.GetService<TInstance>())
{
}
}
public class KeyedSingletonService<TKey, TService> : IKeyedService<TKey, TService>
where TService : class
{
private readonly Lazy<TService> instance;
public TKey Key { get; }
public KeyedSingletonService(TKey key, IServiceProvider services, Func<IServiceProvider, TKey, TService> factory)
{
this.Key = key;
this.instance = new Lazy<TService>(() => factory(services, Key));
}
public TService GetService(IServiceProvider services) => this.instance.Value;
public bool Equals(TKey other)
{
return Equals(Key, other);
}
}
public class KeyedSingletonService<TKey, TService, TInstance> : KeyedSingletonService<TKey, TService>
where TInstance : TService
where TService : class
{
public KeyedSingletonService(TKey key, IServiceProvider services)
: base(key, services, (sp, k) => sp.GetService<TInstance>())
{
}
}
public class KeyedServiceCollection<TKey, TService> : IKeyedServiceCollection<TKey, TService>
where TService : class
{
public TService GetService(IServiceProvider services, TKey key)
{
return this.GetServices(services).FirstOrDefault(s => s.Equals(key))?.GetService(services);
}
public IEnumerable<IKeyedService<TKey, TService>> GetServices(IServiceProvider services)
{
return services.GetServices<IKeyedService<TKey, TService>>();
}
}
public static class KeyedServiceExtensions
{
/// <summary>
/// Register a transient keyed service
/// </summary>
public static IServiceCollection AddTransientKeyedService<TKey, TService>(this IServiceCollection collection, TKey key, Func<IServiceProvider, TKey, TService> factory)
where TService : class
{
return collection.AddSingleton<IKeyedService<TKey, TService>>(sp => new KeyedService<TKey, TService>(key, sp, factory));
}
/// <summary>
/// Register a transient keyed service
/// </summary>
public static IServiceCollection AddTransientKeyedService<TKey, TService, TInstance>(this IServiceCollection collection, TKey key)
where TInstance : class, TService
where TService : class
{
collection.TryAddTransient<TInstance>();
return collection.AddSingleton<IKeyedService<TKey, TService>>(sp => new KeyedService<TKey, TService, TInstance>(key,sp));
}
/// <summary>
/// Register a singleton keyed service
/// </summary>
public static IServiceCollection AddSingletonKeyedService<TKey, TService>(this IServiceCollection collection, TKey key, Func<IServiceProvider, TKey, TService> factory)
where TService : class
{
return collection.AddSingleton<IKeyedService<TKey, TService>>(sp => new KeyedSingletonService<TKey, TService>(key, sp, factory));
}
/// <summary>
/// Register a singleton keyed service
/// </summary>
public static IServiceCollection AddSingletonKeyedService<TKey, TService, TInstance>(this IServiceCollection collection, TKey key)
where TInstance : class, TService
where TService : class
{
collection.TryAddTransient<TInstance>();
return collection.AddSingleton<IKeyedService<TKey, TService>>(sp => new KeyedSingletonService<TKey, TService, TInstance>(key, sp));
}
/// <summary>
/// Register a transient named service
/// </summary>
public static IServiceCollection AddTransientNamedService<TService>(this IServiceCollection collection, string name, Func<IServiceProvider, string, TService> factory)
where TService : class
{
return collection.AddTransientKeyedService<string, TService>(name, factory);
}
/// <summary>
/// Register a transient named service
/// </summary>
public static IServiceCollection AddTransientNamedService<TService, TInstance>(this IServiceCollection collection, string name)
where TInstance : class, TService
where TService : class
{
return collection.AddTransientKeyedService<string, TService, TInstance>(name);
}
/// <summary>
/// Register a singleton named service
/// </summary>
public static IServiceCollection AddSingletonNamedService<TService>(this IServiceCollection collection, string name, Func<IServiceProvider, string, TService> factory)
where TService : class
{
return collection.AddSingletonKeyedService<string, TService>(name, factory);
}
/// <summary>
/// Register a singleton named service
/// </summary>
public static IServiceCollection AddSingletonNamedService<TService, TInstance>(this IServiceCollection collection, string name)
where TInstance : class, TService
where TService : class
{
return collection.AddSingletonKeyedService<string, TService, TInstance>(name);
}
}
}