Skip to content

Commit 4450784

Browse files
authored
Simplify DI Support (#6994)
1 parent 717ded9 commit 4450784

File tree

100 files changed

+1018
-2019
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1018
-2019
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "8.0.202",
3+
"version": "8.0.101",
44
"rollForward": "latestMajor"
55
}
66
}

src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/CertificationSchema/AnnotationBased/SchemaSetup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ public static async Task<IRequestExecutor> CreateAsync()
1313
.AddGraphQL()
1414
.AddApolloFederation()
1515
.AddQueryType<Query>()
16-
.RegisterService<Data>()
1716
.BuildRequestExecutorAsync();
1817
}

src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/CertificationSchema/CodeFirst/SchemaSetup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ public static async Task<IRequestExecutor> CreateAsync()
1313
.AddGraphQL()
1414
.AddApolloFederation()
1515
.AddQueryType<QueryType>()
16-
.RegisterService<Data>()
1716
.BuildRequestExecutorAsync();
1817
}

src/HotChocolate/Core/src/Abstractions/ScopedServiceAttribute.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/HotChocolate/Core/src/Abstractions/ServiceAttribute.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ public class ServiceAttribute : Attribute
1111
/// <summary>
1212
/// Marks a resolver parameter as a service that shall be injected by the execution engine.
1313
/// </summary>
14-
/// <param name="kind">
15-
/// The scope of the service.
16-
/// </param>
17-
public ServiceAttribute(ServiceKind kind = ServiceKind.Default)
14+
public ServiceAttribute()
1815
{
19-
Kind = kind;
2016
}
2117

2218
#if NET8_0_OR_GREATER
@@ -26,24 +22,14 @@ public ServiceAttribute(ServiceKind kind = ServiceKind.Default)
2622
/// <param name="key">
2723
/// A key that shall be used to resolve the service.
2824
/// </param>
29-
/// <param name="kind">
30-
/// The scope of the service.
31-
/// </param>
32-
public ServiceAttribute(string key, ServiceKind kind = ServiceKind.Default)
25+
public ServiceAttribute(string key)
3326
{
3427
Key = key;
35-
Kind = kind;
3628
}
3729

3830
/// <summary>
3931
/// Gets the key that shall be used to resolve the service.
4032
/// </summary>
4133
public string? Key { get; }
4234
#endif
43-
44-
/// <summary>
45-
/// Gets the service kind which specifies the way the service
46-
/// shall be injected and handled by the execution engine.
47-
/// </summary>
48-
public ServiceKind Kind { get; }
4935
}

src/HotChocolate/Core/src/Abstractions/ServiceKind.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/HotChocolate/Core/src/Execution/DependencyInjection/Factories/OperationContextFactory.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using HotChocolate.Execution.DependencyInjection;
33
using HotChocolate.Execution.Processing;
44
using HotChocolate.Execution.Processing.Tasks;
5+
using HotChocolate.Resolvers;
56
using HotChocolate.Utilities;
67

78
namespace Microsoft.Extensions.DependencyInjection;
@@ -17,28 +18,17 @@ namespace Microsoft.Extensions.DependencyInjection;
1718
///
1819
/// The <see cref="OperationContextFactory"/> MUST be a singleton.
1920
/// </summary>
20-
internal sealed class OperationContextFactory : IFactory<OperationContext>
21+
internal sealed class OperationContextFactory(
22+
IFactory<ResolverTask> resolverTaskFactory,
23+
ResultPool resultPool,
24+
ITypeConverter typeConverter,
25+
AggregateServiceScopeInitializer serviceScopeInitializer)
26+
: IFactory<OperationContext>
2127
{
22-
private readonly IFactory<ResolverTask> _resolverTaskFactory;
23-
private readonly ResultPool _resultPool;
24-
private readonly ITypeConverter _typeConverter;
25-
26-
public OperationContextFactory(
27-
IFactory<ResolverTask> resolverTaskFactory,
28-
ResultPool resultPool,
29-
ITypeConverter typeConverter)
30-
{
31-
_resolverTaskFactory = resolverTaskFactory ??
32-
throw new ArgumentNullException(nameof(resolverTaskFactory));
33-
_resultPool = resultPool ??
34-
throw new ArgumentNullException(nameof(resultPool));
35-
_typeConverter = typeConverter ??
36-
throw new ArgumentNullException(nameof(typeConverter));
37-
}
38-
3928
public OperationContext Create()
4029
=> new OperationContext(
41-
_resolverTaskFactory,
42-
new ResultBuilder(_resultPool),
43-
_typeConverter);
30+
resolverTaskFactory,
31+
new ResultBuilder(resultPool),
32+
typeConverter,
33+
serviceScopeInitializer);
4434
}

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.ResolverCompiler.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using HotChocolate.Execution.Configuration;
3+
using HotChocolate.Resolvers;
4+
5+
// ReSharper disable once CheckNamespace
6+
namespace Microsoft.Extensions.DependencyInjection;
7+
8+
public static partial class RequestExecutorBuilderExtensions
9+
{
10+
/// <summary>
11+
/// Adds a initializer to copy state between a request scoped service instance and
12+
/// a resolver scoped service instance.
13+
/// </summary>
14+
/// <param name="builder">
15+
/// The request executor builder.
16+
/// </param>
17+
/// <param name="initializer">
18+
/// The initializer that is used to initialize the service scope.
19+
/// </param>
20+
/// <typeparam name="TService">
21+
/// The type of the service that shall be initialized.
22+
/// </typeparam>
23+
/// <returns></returns>
24+
/// <exception cref="ArgumentNullException">
25+
/// The <paramref name="builder"/> is <c>null</c>.
26+
/// </exception>
27+
public static IRequestExecutorBuilder AddScopedServiceInitializer<TService>(
28+
this IRequestExecutorBuilder builder,
29+
Action<TService, TService> initializer)
30+
{
31+
if (builder == null)
32+
{
33+
throw new ArgumentNullException(nameof(builder));
34+
}
35+
36+
if (initializer == null)
37+
{
38+
throw new ArgumentNullException(nameof(initializer));
39+
}
40+
41+
builder.Services.AddSingleton<IServiceScopeInitializer>(new DelegateServiceInitializer<TService>(initializer));
42+
return builder;
43+
}
44+
}
45+
46+
file sealed class DelegateServiceInitializer<TService>(
47+
Action<TService, TService> initializer)
48+
: ServiceInitializer<TService>
49+
{
50+
private readonly Action<TService, TService> _initializer = initializer ??
51+
throw new ArgumentNullException(nameof(initializer));
52+
53+
protected override void Initialize(TService requestScopeService, TService resolverScopeService)
54+
=> _initializer(requestScopeService, resolverScopeService);
55+
}

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using HotChocolate.Execution.Processing;
1111
using HotChocolate.Fetching;
1212
using HotChocolate.Language;
13+
using HotChocolate.Resolvers;
1314
using Microsoft.Extensions.DependencyInjection.Extensions;
1415
using Microsoft.Extensions.ObjectPool;
1516

@@ -36,6 +37,7 @@ public static IServiceCollection AddGraphQLCore(this IServiceCollection services
3637
services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
3738
services.TryAddSingleton<DefaultRequestContextAccessor>();
3839
services.TryAddSingleton<IRequestContextAccessor>(sp => sp.GetRequiredService<DefaultRequestContextAccessor>());
40+
services.TryAddSingleton<AggregateServiceScopeInitializer>();
3941

4042
services.TryAddSingleton<ObjectPool<StringBuilder>>(sp =>
4143
{

0 commit comments

Comments
 (0)