Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed GreenDonut standalone DI configuration. #6961

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,45 @@ public static IServiceCollection AddDataLoader<T>(
services.TryAddScoped<T>(sp => sp.GetDataLoader<T>());
return services;
}

public static IServiceCollection TryAddDataLoaderCore(
this IServiceCollection services)
{
services.TryAddScoped<IDataLoaderScope, DefaultDataLoaderScope>();
services.TryAddScoped<IBatchScheduler, AutoBatchScheduler>();

services.TryAddSingleton(sp => TaskCachePool.Create(sp.GetRequiredService<ObjectPoolProvider>()));
services.TryAddScoped(sp => new TaskCacheOwner(sp.GetRequiredService<ObjectPool<TaskCache>>()));

services.TryAddSingleton<IDataLoaderDiagnosticEvents>(
sp =>
{
var listeners = sp.GetServices<IDataLoaderDiagnosticEventListener>().ToArray();

return listeners.Length switch
{
0 => new DataLoaderDiagnosticEventListener(),
1 => listeners[0],
_ => new AggregateDataLoaderDiagnosticEventListener(listeners),
};
});

services.TryAddScoped(
sp =>
{
var cacheOwner = sp.GetRequiredService<TaskCacheOwner>();

return new DataLoaderOptions
{
Cache = cacheOwner.Cache,
CancellationToken = cacheOwner.CancellationToken,
DiagnosticEvents = sp.GetService<IDataLoaderDiagnosticEvents>(),
MaxBatchSize = 1024,
};
});

return services;
services.AddSingleton<DataLoaderScopeFactory>();
services.TryAddScoped<IDataLoaderScope>(sp => sp.GetRequiredService<DataLoaderScopeFactory>().CreateScope(sp));
services.TryAddScoped<IBatchScheduler, AutoBatchScheduler>();

services.TryAddSingleton(sp => TaskCachePool.Create(sp.GetRequiredService<ObjectPoolProvider>()));
services.TryAddScoped(sp => new TaskCacheOwner(sp.GetRequiredService<ObjectPool<TaskCache>>()));

services.TryAddSingleton<IDataLoaderDiagnosticEvents>(
sp =>
{
var listeners = sp.GetServices<IDataLoaderDiagnosticEventListener>().ToArray();

return listeners.Length switch
{
0 => new DataLoaderDiagnosticEventListener(),
1 => listeners[0],
_ => new AggregateDataLoaderDiagnosticEventListener(listeners),
};
});

services.TryAddScoped(
sp =>
{
var cacheOwner = sp.GetRequiredService<TaskCacheOwner>();

return new DataLoaderOptions
{
Cache = cacheOwner.Cache,
CancellationToken = cacheOwner.CancellationToken,
DiagnosticEvents = sp.GetService<IDataLoaderDiagnosticEvents>(),
MaxBatchSize = 1024,
};
});

return services;
}
}

Expand All @@ -93,6 +94,25 @@ public static T GetDataLoader<T>(this IServiceProvider services) where T : IData
=> services.GetRequiredService<IDataLoaderScope>().GetDataLoader<T>();
}

internal sealed class DataLoaderScopeFactory
{
#if NET8_0_OR_GREATER
private readonly FrozenDictionary<Type, DataLoaderRegistration> _registrations;
#else
private readonly Dictionary<Type, DataLoaderRegistration> _registrations;
#endif

public DataLoaderScopeFactory(IEnumerable<DataLoaderRegistration> dataLoaderRegistrations)
#if NET8_0_OR_GREATER
=> _registrations = dataLoaderRegistrations.ToFrozenDictionary(t => t.ServiceType);
#else
=> _registrations = dataLoaderRegistrations.ToDictionary(t => t.ServiceType);
#endif

public IDataLoaderScope CreateScope(IServiceProvider scopedServiceProvider)
=> new DefaultDataLoaderScope(scopedServiceProvider, _registrations);
}

file sealed class DefaultDataLoaderScope(
IServiceProvider serviceProvider,
#if NET8_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ public interface IDataLoaderScope
/// Returns a <see cref="IDataLoader"/> instance from the current execution scope.
/// </returns>
T GetDataLoader<T>() where T : IDataLoader;
}
}

Loading