1- // Licensed to the .NET Foundation under one or more agreements.
2- // The .NET Foundation licenses this file to you under the MIT license.
1+ //
2+ // Copyright (c) .NET Foundation and Contributors
3+ // See LICENSE file in the project root for full license information.
4+ //
35
46using System ;
57using System . Collections ;
68
79namespace nanoFramework . DependencyInjection
810{
11+ /// <summary>
12+ /// Extensions for <see cref="ServiceCollection"/>.
13+ /// </summary>
914 public static class ServiceCollectionServiceExtensions
1015 {
1116 /// <summary>
@@ -18,7 +23,7 @@ public static class ServiceCollectionServiceExtensions
1823 /// <param name="implementationType">The implementation type of the service.</param>
1924 /// <returns>A reference to this instance after the operation has completed.</returns>
2025 /// <seealso cref="ServiceLifetime.Singleton"/>
21- /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be nul .</exception>
26+ /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be <see langword="null"/> .</exception>
2227 public static IServiceCollection AddSingleton ( this IServiceCollection services , Type serviceType , Type implementationType )
2328 {
2429 if ( services == null )
@@ -40,7 +45,7 @@ public static IServiceCollection AddSingleton(this IServiceCollection services,
4045 /// <param name="serviceType">The type of the service to register and the implementation to use.</param>
4146 /// <returns>A reference to this instance after the operation has completed.</returns>
4247 /// <seealso cref="ServiceLifetime.Singleton"/>
43- /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be null.</exception>
48+ /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be <see langword=" null"/> .</exception>
4449 public static IServiceCollection AddSingleton ( this IServiceCollection services , Type serviceType )
4550 {
4651 if ( services == null )
@@ -61,7 +66,7 @@ public static IServiceCollection AddSingleton(this IServiceCollection services,
6166 /// <param name="implementationInstance">The instance of the service.</param>
6267 /// <returns>A reference to this instance after the operation has completed.</returns>
6368 /// <seealso cref="ServiceLifetime.Singleton"/>
64- /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be null.</exception>
69+ /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be <see langword=" null"/> .</exception>
6570 public static IServiceCollection AddSingleton ( this IServiceCollection services , Type serviceType , object implementationInstance )
6671 {
6772 if ( services == null )
@@ -71,6 +76,7 @@ public static IServiceCollection AddSingleton(this IServiceCollection services,
7176
7277 var serviceDescriptor = new ServiceDescriptor ( serviceType , implementationInstance ) ;
7378 services . Add ( serviceDescriptor ) ;
79+
7480 return services ;
7581 }
7682
@@ -84,7 +90,7 @@ public static IServiceCollection AddSingleton(this IServiceCollection services,
8490 /// <param name="implementationType">The implementation type of the service.</param>
8591 /// <returns>A reference to this instance after the operation has completed.</returns>
8692 /// <seealso cref="ServiceLifetime.Transient"/>
87- /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be null.</exception>
93+ /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be <see langword=" null"/> .</exception>
8894 public static IServiceCollection AddTransient ( this IServiceCollection services , Type serviceType , Type implementationType )
8995 {
9096 if ( services == null )
@@ -106,7 +112,7 @@ public static IServiceCollection AddTransient(this IServiceCollection services,
106112 /// <param name="serviceType">The type of the service to register and the implementation to use.</param>
107113 /// <returns>A reference to this instance after the operation has completed.</returns>
108114 /// <seealso cref="ServiceLifetime.Transient"/>
109- /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be null.</exception>
115+ /// <exception cref="ArgumentNullException"><paramref name="services"/> can't be <see langword=" null"/> .</exception>
110116 public static IServiceCollection AddTransient ( this IServiceCollection services , Type serviceType )
111117 {
112118 if ( services == null )
@@ -123,7 +129,7 @@ public static IServiceCollection AddTransient(this IServiceCollection services,
123129 /// </summary>
124130 /// <param name="collection">The <see cref="IServiceCollection"/>.</param>
125131 /// <param name="descriptor">The <see cref="ServiceDescriptor"/> to add.</param>
126- /// <exception cref="ArgumentNullException"><paramref name="collection"/> or <paramref name="descriptor"/> can't be null.</exception>
132+ /// <exception cref="ArgumentNullException"><paramref name="collection"/> or <paramref name="descriptor"/> can't be <see langword=" null"/> .</exception>
127133 public static void TryAdd ( this IServiceCollection collection , ServiceDescriptor descriptor )
128134 {
129135 if ( collection == null )
@@ -137,6 +143,7 @@ public static void TryAdd(this IServiceCollection collection, ServiceDescriptor
137143 }
138144
139145 int count = collection . Count ;
146+
140147 for ( int index = 0 ; index < count ; index ++ )
141148 {
142149 if ( collection [ index ] . ServiceType == descriptor . ServiceType )
@@ -167,7 +174,7 @@ public static void TryAdd(this IServiceCollection collection, ServiceDescriptor
167174 /// of multiple implementation types.
168175 /// </remarks>
169176 /// <exception cref="ArgumentException">Implementation type cannot be 'implementationType' because it is indistinguishable from other services registered for 'descriptor.ServiceType'.</exception>
170- /// <exception cref="ArgumentNullException"><paramref name="services"/> or <paramref name="descriptor"/> can't be null.</exception>
177+ /// <exception cref="ArgumentNullException"><paramref name="services"/> or <paramref name="descriptor"/> can't be <see langword=" null"/> .</exception>
171178 public static void TryAddEnumerable ( this IServiceCollection services , ServiceDescriptor descriptor )
172179 {
173180 if ( services == null )
@@ -182,18 +189,20 @@ public static void TryAddEnumerable(this IServiceCollection services, ServiceDes
182189
183190 Type implementationType = descriptor . GetImplementationType ( ) ;
184191
185- if ( implementationType == typeof ( object ) ||
186- implementationType == descriptor . ServiceType )
192+ if ( implementationType == typeof ( object )
193+ || implementationType == descriptor . ServiceType )
187194 {
188195 throw new ArgumentException ( ) ;
189196 }
190197
191198 int count = services . Count ;
199+
192200 for ( int index = 0 ; index < count ; index ++ )
193201 {
194202 ServiceDescriptor service = services [ index ] ;
195- if ( service . ServiceType == descriptor . ServiceType &&
196- service . GetImplementationType ( ) == implementationType )
203+
204+ if ( service . ServiceType == descriptor . ServiceType
205+ && service . GetImplementationType ( ) == implementationType )
197206 {
198207 // Already added
199208 return ;
@@ -220,7 +229,7 @@ public static void TryAddEnumerable(this IServiceCollection services, ServiceDes
220229 /// <see cref="TryAddEnumerable(IServiceCollection, ServiceDescriptor)"/> will prevent registration
221230 /// of multiple implementation types.
222231 /// </remarks>
223- /// <exception cref="ArgumentNullException"><paramref name="services"/> or <paramref name="descriptors"/> can't be null.</exception>
232+ /// <exception cref="ArgumentNullException"><paramref name="services"/> or <paramref name="descriptors"/> can't be <see langword=" null"/> .</exception>
224233 public static void TryAddEnumerable ( this IServiceCollection services , IEnumerable descriptors )
225234 {
226235 if ( services == null )
@@ -246,7 +255,7 @@ public static void TryAddEnumerable(this IServiceCollection services, IEnumerabl
246255 /// <param name="collection">The <see cref="IServiceCollection"/>.</param>
247256 /// <param name="descriptor">The <see cref="ServiceDescriptor"/> to replace with.</param>
248257 /// <returns>The <see cref="IServiceCollection"/> for chaining.</returns>
249- /// <exception cref="ArgumentNullException"><paramref name="collection"/> or <paramref name="descriptor"/> can't be null.</exception>
258+ /// <exception cref="ArgumentNullException"><paramref name="collection"/> or <paramref name="descriptor"/> can't be <see langword=" null"/> .</exception>
250259 public static IServiceCollection Replace ( this IServiceCollection collection , ServiceDescriptor descriptor )
251260 {
252261 if ( collection == null )
@@ -261,6 +270,7 @@ public static IServiceCollection Replace(this IServiceCollection collection, Ser
261270
262271 // Remove existing
263272 int count = collection . Count ;
273+
264274 for ( int index = 0 ; index < count ; index ++ )
265275 {
266276 if ( collection [ index ] . ServiceType == descriptor . ServiceType )
@@ -271,6 +281,7 @@ public static IServiceCollection Replace(this IServiceCollection collection, Ser
271281 }
272282
273283 collection . Add ( descriptor ) ;
284+
274285 return collection ;
275286 }
276287
@@ -280,7 +291,7 @@ public static IServiceCollection Replace(this IServiceCollection collection, Ser
280291 /// <param name="collection">The <see cref="IServiceCollection"/>.</param>
281292 /// <param name="serviceType">The service type to remove.</param>
282293 /// <returns>The <see cref="IServiceCollection"/> for chaining.</returns>
283- /// <exception cref="ArgumentNullException"><paramref name="serviceType"/> can't be null.</exception>
294+ /// <exception cref="ArgumentNullException"><paramref name="serviceType"/> can't be <see langword=" null"/> .</exception>
284295 public static IServiceCollection RemoveAll ( this IServiceCollection collection , Type serviceType )
285296 {
286297 if ( serviceType == null )
@@ -291,6 +302,7 @@ public static IServiceCollection RemoveAll(this IServiceCollection collection, T
291302 for ( int index = collection . Count - 1 ; index >= 0 ; index -- )
292303 {
293304 ServiceDescriptor descriptor = collection [ index ] ;
305+
294306 if ( descriptor . ServiceType == serviceType )
295307 {
296308 collection . RemoveAt ( index ) ;
@@ -300,4 +312,4 @@ public static IServiceCollection RemoveAll(this IServiceCollection collection, T
300312 return collection ;
301313 }
302314 }
303- }
315+ }
0 commit comments