Skip to content

Commit 315116a

Browse files
authored
Fixed DataLoader interface injection. (#8380)
1 parent a8d09ec commit 315116a

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

src/GreenDonut/src/GreenDonut/DependencyInjection/DataLoaderServiceCollectionExtensions.cs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,112 @@
77
// ReSharper disable once CheckNamespace
88
namespace Microsoft.Extensions.DependencyInjection;
99

10+
/// <summary>
11+
/// Extension methods for <see cref="IServiceCollection"/> to add data loader services.
12+
/// </summary>
1013
public static class DataLoaderServiceCollectionExtensions
1114
{
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>
1221
public static IServiceCollection AddDataLoader<T>(
1322
this IServiceCollection services)
1423
where T : class, IDataLoader
1524
{
25+
ArgumentNullException.ThrowIfNull(services);
26+
1627
services.TryAddDataLoaderCore();
1728
services.AddSingleton(new DataLoaderRegistration(typeof(T)));
18-
services.TryAddScoped<T>(sp => sp.GetDataLoader<T>());
29+
services.TryAddScoped(sp => sp.GetDataLoader<T>());
1930
return services;
2031
}
2132

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>
2241
public static IServiceCollection AddDataLoader<TService, TImplementation>(
2342
this IServiceCollection services)
2443
where TService : class, IDataLoader
2544
where TImplementation : class, TService
2645
{
46+
ArgumentNullException.ThrowIfNull(services);
47+
2748
services.TryAddDataLoaderCore();
2849
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>());
3152
return services;
3253
}
3354

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>
3462
public static IServiceCollection AddDataLoader<T>(
3563
this IServiceCollection services,
3664
Func<IServiceProvider, T> factory)
3765
where T : class, IDataLoader
3866
{
67+
ArgumentNullException.ThrowIfNull(services);
68+
ArgumentNullException.ThrowIfNull(factory);
69+
3970
services.TryAddDataLoaderCore();
4071
services.AddSingleton(new DataLoaderRegistration(typeof(T), sp => factory(sp)));
41-
services.TryAddScoped<T>(sp => sp.GetDataLoader<T>());
72+
services.TryAddScoped(sp => sp.GetDataLoader<T>());
4273
return services;
4374
}
4475

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>
4585
public static IServiceCollection AddDataLoader<TService, TImplementation>(
4686
this IServiceCollection services,
4787
Func<IServiceProvider, TImplementation> factory)
4888
where TService : class, IDataLoader
4989
where TImplementation : class, TService
5090
{
91+
ArgumentNullException.ThrowIfNull(services);
92+
ArgumentNullException.ThrowIfNull(factory);
93+
5194
services.TryAddDataLoaderCore();
5295
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>());
5598
return services;
5699
}
57100

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>
58106
public static IServiceCollection TryAddDataLoaderCore(
59107
this IServiceCollection services)
60108
{
109+
ArgumentNullException.ThrowIfNull(services);
110+
61111
services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
62112

63113
services.TryAddSingleton<DataLoaderRegistrar>();
64114
services.AddSingleton<DataLoaderScopeFactory>();
65-
services.TryAddScoped<IDataLoaderScope>(sp => sp.GetRequiredService<DataLoaderScopeFactory>().CreateScope(sp));
115+
services.TryAddScoped(sp => sp.GetRequiredService<DataLoaderScopeFactory>().CreateScope(sp));
66116
services.TryAddScoped<IBatchScheduler, AutoBatchScheduler>();
67117

68118
services.TryAddSingleton(sp => PromiseCachePool.Create(sp.GetRequiredService<ObjectPoolProvider>()));

0 commit comments

Comments
 (0)