This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
/
ServiceCollectionExtensions.cs
248 lines (217 loc) · 11.7 KB
/
ServiceCollectionExtensions.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds a sequence of <see cref="ServiceDescriptor"/> to the <paramref name="collection"/>.
/// </summary>
/// <param name="descriptor">The <see cref="IEnumerable{T}"/> of <see cref="ServiceDescriptor"/>s to add.</param>
/// <returns>A reference to the current instance of <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection Add([NotNull] this IServiceCollection collection,
[NotNull] IEnumerable<ServiceDescriptor> descriptors)
{
foreach (var descriptor in descriptors)
{
collection.Add(descriptor);
}
return collection;
}
/// <summary>
/// Adds the specified <paramref name="descriptor"/> to the <paramref name="collection"/>.
/// </summary>
/// <param name="collection">The <see cref="IServiceCollection"/>.</param>
/// <param name="descriptor">The <see cref="ServiceDescriptor"/>.</param>
/// <returns>A reference to the current instance of <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection Add([NotNull] this IServiceCollection collection,
[NotNull] ServiceDescriptor descriptor)
{
collection.Add(descriptor);
return collection;
}
/// <summary>
/// Adds the specified <paramref name="descriptor"/> to the <paramref name="collection"/> if the
/// service type hasn't been already registered.
/// </summary>
/// <param name="collection">The <see cref="IServiceCollection"/>.</param>
/// <param name="descriptor">The <see cref="ServiceDescriptor"/>.</param>
/// <returns><c>true</c> if the <paramref name="descriptor"/> was added; otherwise <c>false</c>.</returns>
public static bool TryAdd([NotNull] this IServiceCollection collection,
[NotNull] ServiceDescriptor descriptor)
{
if (!collection.Any(d => d.ServiceType == descriptor.ServiceType))
{
collection.Add(descriptor);
return true;
}
return false;
}
/// <summary>
/// Adds the specified <paramref name="descriptor"/>s to the <paramref name="collection"/> if the
/// service type hasn't been already registered.
/// </summary>
/// <param name="collection">The <see cref="IServiceCollection"/>.</param>
/// <param name="descriptor">The <see cref="ServiceDescriptor"/>s.</param>
/// <returns><c>true</c> if any of the <paramref name="descriptor"/>s was added; otherwise <c>false</c>.</returns>
public static bool TryAdd([NotNull] this IServiceCollection collection,
[NotNull] IEnumerable<ServiceDescriptor> descriptors)
{
var anyAdded = false;
foreach (var d in descriptors)
{
anyAdded = collection.TryAdd(d) || anyAdded;
}
return anyAdded;
}
public static IServiceCollection AddTransient([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Type implementationType)
{
return Add(collection, service, implementationType, ServiceLifetime.Transient);
}
public static IServiceCollection AddTransient([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Func<IServiceProvider, object> implementationFactory)
{
return Add(collection, service, implementationFactory, ServiceLifetime.Transient);
}
public static IServiceCollection AddScoped([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Type implementationType)
{
return Add(collection, service, implementationType, ServiceLifetime.Scoped);
}
public static IServiceCollection AddScoped([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Func<IServiceProvider, object> implementationFactory)
{
return Add(collection, service, implementationFactory, ServiceLifetime.Scoped);
}
public static IServiceCollection AddSingleton([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Type implementationType)
{
return Add(collection, service, implementationType, ServiceLifetime.Singleton);
}
public static IServiceCollection AddSingleton([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] Func<IServiceProvider, object> implementationFactory)
{
return Add(collection, service, implementationFactory, ServiceLifetime.Singleton);
}
public static IServiceCollection AddInstance([NotNull] this IServiceCollection collection,
[NotNull] Type service,
[NotNull] object implementationInstance)
{
var serviceDescriptor = new ServiceDescriptor(service, implementationInstance);
collection.Add(serviceDescriptor);
return collection;
}
public static IServiceCollection AddTransient<TService, TImplementation>([NotNull] this IServiceCollection services)
where TImplementation : TService
{
return services.AddTransient(typeof(TService), typeof(TImplementation));
}
public static IServiceCollection AddTransient([NotNull] this IServiceCollection services,
[NotNull] Type serviceType)
{
return services.AddTransient(serviceType, serviceType);
}
public static IServiceCollection AddTransient<TService>([NotNull] this IServiceCollection services)
{
return services.AddTransient(typeof(TService));
}
public static IServiceCollection AddTransient<TService>([NotNull] this IServiceCollection services,
[NotNull] Func<IServiceProvider, TService> implementationFactory)
where TService : class
{
return services.AddTransient(typeof(TService), implementationFactory);
}
public static IServiceCollection AddScoped<TService, TImplementation>([NotNull] this IServiceCollection services)
where TImplementation : TService
{
return services.AddScoped(typeof(TService), typeof(TImplementation));
}
public static IServiceCollection AddScoped([NotNull] this IServiceCollection services,
[NotNull] Type serviceType)
{
return services.AddScoped(serviceType, serviceType);
}
public static IServiceCollection AddScoped<TService>([NotNull] this IServiceCollection services,
[NotNull] Func<IServiceProvider, TService> implementationFactory)
where TService : class
{
return services.AddScoped(typeof(TService), implementationFactory);
}
public static IServiceCollection AddScoped<TService>([NotNull] this IServiceCollection services)
{
return services.AddScoped(typeof(TService));
}
public static IServiceCollection AddSingleton<TService, TImplementation>([NotNull] this IServiceCollection services)
{
return services.AddSingleton(typeof(TService), typeof(TImplementation));
}
public static IServiceCollection AddSingleton([NotNull] this IServiceCollection services,
[NotNull] Type serviceType)
{
return services.AddSingleton(serviceType, serviceType);
}
public static IServiceCollection AddSingleton<TService>([NotNull] this IServiceCollection services)
{
return services.AddSingleton(typeof(TService));
}
public static IServiceCollection AddSingleton<TService>([NotNull] this IServiceCollection services,
[NotNull] Func<IServiceProvider, TService> implementationFactory)
where TService : class
{
return services.AddSingleton(typeof(TService), implementationFactory);
}
public static IServiceCollection AddInstance<TService>([NotNull] this IServiceCollection services,
[NotNull] TService implementationInstance)
where TService : class
{
return services.AddInstance(typeof(TService), implementationInstance);
}
/// <summary>
/// Removes the first service in <see cref="IServiceCollection"/> with the same service type
/// as <paramref name="descriptor"/> and adds <paramef name="descriptor"/> to the collection.
/// </summary>
/// <param name="collection">The <see cref="IServiceCollection"/>.</param>
/// <param name="descriptor">The <see cref="ServiceDescriptor"/> to replace with.</param>
/// <returns></returns>
public static IServiceCollection Replace([NotNull] this IServiceCollection collection,
[NotNull] ServiceDescriptor descriptor)
{
var registeredServiceDescriptor = collection.FirstOrDefault(s => s.ServiceType == descriptor.ServiceType);
if (registeredServiceDescriptor != null)
{
collection.Remove(registeredServiceDescriptor);
}
collection.Add(descriptor);
return collection;
}
private static IServiceCollection Add(IServiceCollection collection,
Type service,
Type implementationType,
ServiceLifetime lifeCycle)
{
var descriptor = new ServiceDescriptor(service, implementationType, lifeCycle);
collection.Add(descriptor);
return collection;
}
private static IServiceCollection Add(IServiceCollection collection,
Type service,
Func<IServiceProvider, object> implementationFactory,
ServiceLifetime lifeCycle)
{
var descriptor = new ServiceDescriptor(service, implementationFactory, lifeCycle);
collection.Add(descriptor);
return collection;
}
}
}