|
7 | 7 | // ReSharper disable once CheckNamespace
|
8 | 8 | namespace Microsoft.Extensions.DependencyInjection;
|
9 | 9 |
|
| 10 | +/// <summary> |
| 11 | +/// Extension methods for <see cref="IServiceCollection"/> to add data loader services. |
| 12 | +/// </summary> |
10 | 13 | public static class DataLoaderServiceCollectionExtensions
|
11 | 14 | {
|
| 15 | + /// <summary> |
| 16 | + /// Adds a data loader for the specified type <typeparamref name="T"/>. |
| 17 | + /// </summary> |
| 18 | + /// <typeparam name="T">The type of the data loader to add.</typeparam> |
| 19 | + /// <param name="services">The service collection to add the data loader to.</param> |
| 20 | + /// <returns>The service collection.</returns> |
12 | 21 | public static IServiceCollection AddDataLoader<T>(
|
13 | 22 | this IServiceCollection services)
|
14 | 23 | where T : class, IDataLoader
|
15 | 24 | {
|
| 25 | + ArgumentNullException.ThrowIfNull(services); |
| 26 | + |
16 | 27 | services.TryAddDataLoaderCore();
|
17 | 28 | services.AddSingleton(new DataLoaderRegistration(typeof(T)));
|
18 |
| - services.TryAddScoped<T>(sp => sp.GetDataLoader<T>()); |
| 29 | + services.TryAddScoped(sp => sp.GetDataLoader<T>()); |
19 | 30 | return services;
|
20 | 31 | }
|
21 | 32 |
|
| 33 | + /// <summary> |
| 34 | + /// Adds a data loader for the specified type <typeparamref name="TService"/> |
| 35 | + /// and <typeparamref name="TImplementation"/>. |
| 36 | + /// </summary> |
| 37 | + /// <typeparam name="TService">The service type of the data loader to add.</typeparam> |
| 38 | + /// <typeparam name="TImplementation">The implementation type of the data loader to add.</typeparam> |
| 39 | + /// <param name="services">The service collection to add the data loader to.</param> |
| 40 | + /// <returns>The service collection.</returns> |
22 | 41 | public static IServiceCollection AddDataLoader<TService, TImplementation>(
|
23 | 42 | this IServiceCollection services)
|
24 | 43 | where TService : class, IDataLoader
|
25 | 44 | where TImplementation : class, TService
|
26 | 45 | {
|
| 46 | + ArgumentNullException.ThrowIfNull(services); |
| 47 | + |
27 | 48 | services.TryAddDataLoaderCore();
|
28 | 49 | services.AddSingleton(new DataLoaderRegistration(typeof(TService), typeof(TImplementation)));
|
29 |
| - services.TryAddScoped<TImplementation>(sp => sp.GetDataLoader<TImplementation>()); |
30 |
| - services.TryAddScoped<TService>(sp => sp.GetDataLoader<TService>()); |
| 50 | + services.TryAddScoped(sp => sp.GetDataLoader<TImplementation>()); |
| 51 | + services.TryAddScoped<TService>(sp => sp.GetDataLoader<TImplementation>()); |
31 | 52 | return services;
|
32 | 53 | }
|
33 | 54 |
|
| 55 | + /// <summary> |
| 56 | + /// Adds a data loader for the specified type <typeparamref name="T"/>. |
| 57 | + /// </summary> |
| 58 | + /// <typeparam name="T">The type of the data loader to add.</typeparam> |
| 59 | + /// <param name="services">The service collection to add the data loader to.</param> |
| 60 | + /// <param name="factory">The factory to create the data loader.</param> |
| 61 | + /// <returns>The service collection.</returns> |
34 | 62 | public static IServiceCollection AddDataLoader<T>(
|
35 | 63 | this IServiceCollection services,
|
36 | 64 | Func<IServiceProvider, T> factory)
|
37 | 65 | where T : class, IDataLoader
|
38 | 66 | {
|
| 67 | + ArgumentNullException.ThrowIfNull(services); |
| 68 | + ArgumentNullException.ThrowIfNull(factory); |
| 69 | + |
39 | 70 | services.TryAddDataLoaderCore();
|
40 | 71 | services.AddSingleton(new DataLoaderRegistration(typeof(T), sp => factory(sp)));
|
41 |
| - services.TryAddScoped<T>(sp => sp.GetDataLoader<T>()); |
| 72 | + services.TryAddScoped(sp => sp.GetDataLoader<T>()); |
42 | 73 | return services;
|
43 | 74 | }
|
44 | 75 |
|
| 76 | + /// <summary> |
| 77 | + /// Adds a data loader for the specified type <typeparamref name="TService"/> |
| 78 | + /// and <typeparamref name="TImplementation"/>. |
| 79 | + /// </summary> |
| 80 | + /// <typeparam name="TService">The service type of the data loader to add.</typeparam> |
| 81 | + /// <typeparam name="TImplementation">The implementation type of the data loader to add.</typeparam> |
| 82 | + /// <param name="services">The service collection to add the data loader to.</param> |
| 83 | + /// <param name="factory">The factory to create the data loader.</param> |
| 84 | + /// <returns>The service collection.</returns> |
45 | 85 | public static IServiceCollection AddDataLoader<TService, TImplementation>(
|
46 | 86 | this IServiceCollection services,
|
47 | 87 | Func<IServiceProvider, TImplementation> factory)
|
48 | 88 | where TService : class, IDataLoader
|
49 | 89 | where TImplementation : class, TService
|
50 | 90 | {
|
| 91 | + ArgumentNullException.ThrowIfNull(services); |
| 92 | + ArgumentNullException.ThrowIfNull(factory); |
| 93 | + |
51 | 94 | services.TryAddDataLoaderCore();
|
52 | 95 | services.AddSingleton(new DataLoaderRegistration(typeof(TService), typeof(TImplementation), sp => factory(sp)));
|
53 |
| - services.TryAddScoped<TImplementation>(sp => sp.GetDataLoader<TImplementation>()); |
54 |
| - services.TryAddScoped<TService>(sp => sp.GetDataLoader<TService>()); |
| 96 | + services.TryAddScoped(sp => sp.GetDataLoader<TImplementation>()); |
| 97 | + services.TryAddScoped<TService>(sp => sp.GetDataLoader<TImplementation>()); |
55 | 98 | return services;
|
56 | 99 | }
|
57 | 100 |
|
| 101 | + /// <summary> |
| 102 | + /// Tries to add the core data loader services to the service collection. |
| 103 | + /// </summary> |
| 104 | + /// <param name="services">The service collection to add the data loader to.</param> |
| 105 | + /// <returns>The service collection.</returns> |
58 | 106 | public static IServiceCollection TryAddDataLoaderCore(
|
59 | 107 | this IServiceCollection services)
|
60 | 108 | {
|
| 109 | + ArgumentNullException.ThrowIfNull(services); |
| 110 | + |
61 | 111 | services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
|
62 | 112 |
|
63 | 113 | services.TryAddSingleton<DataLoaderRegistrar>();
|
64 | 114 | services.AddSingleton<DataLoaderScopeFactory>();
|
65 |
| - services.TryAddScoped<IDataLoaderScope>(sp => sp.GetRequiredService<DataLoaderScopeFactory>().CreateScope(sp)); |
| 115 | + services.TryAddScoped(sp => sp.GetRequiredService<DataLoaderScopeFactory>().CreateScope(sp)); |
66 | 116 | services.TryAddScoped<IBatchScheduler, AutoBatchScheduler>();
|
67 | 117 |
|
68 | 118 | services.TryAddSingleton(sp => PromiseCachePool.Create(sp.GetRequiredService<ObjectPoolProvider>()));
|
|
0 commit comments