@@ -56,6 +56,41 @@ public void ConfigureJsonApiOptions(Action<JsonApiOptions> configureOptions)
56
56
configureOptions ? . Invoke ( _options ) ;
57
57
}
58
58
59
+ /// <summary>
60
+ /// Registers services that are required for the configuration of JsonApiDotNetCore during the start up.
61
+ /// </summary>
62
+ public void RegisterJsonApiStartupServices ( )
63
+ {
64
+ _services . AddSingleton < IJsonApiOptions > ( _options ) ;
65
+ _services . TryAddSingleton < IJsonApiRoutingConvention , JsonApiRoutingConvention > ( ) ;
66
+ _services . TryAddSingleton < IResourceGraphBuilder , ResourceGraphBuilder > ( ) ;
67
+ _services . TryAddSingleton < IServiceDiscoveryFacade > ( sp => new ServiceDiscoveryFacade ( _services , sp . GetRequiredService < IResourceGraphBuilder > ( ) ) ) ;
68
+ _services . TryAddScoped < IJsonApiExceptionFilterProvider , JsonApiExceptionFilterProvider > ( ) ;
69
+ _services . TryAddScoped < IJsonApiTypeMatchFilterProvider , JsonApiTypeMatchFilterProvider > ( ) ;
70
+ }
71
+
72
+ public void ConfigureAutoDiscovery ( Action < IServiceDiscoveryFacade > configureAutoDiscovery )
73
+ {
74
+ var intermediateProvider = _services . BuildServiceProvider ( ) ;
75
+ _serviceDiscoveryFacade = intermediateProvider . GetRequiredService < IServiceDiscoveryFacade > ( ) ;
76
+ _resourceGraphBuilder = intermediateProvider . GetRequiredService < IResourceGraphBuilder > ( ) ;
77
+ RegisterDiscoverableAssemblies ( configureAutoDiscovery , _serviceDiscoveryFacade ) ;
78
+ _intermediateServiceProviders . Add ( intermediateProvider ) ;
79
+ }
80
+
81
+ /// <summary>
82
+ /// Configures and build the resource graph with resources from the provided sources and adds it to the DI container.
83
+ /// </summary>
84
+ public void AddResourceGraph ( Type dbContextType , Action < IResourceGraphBuilder > configureResources )
85
+ {
86
+ var intermediateProvider = _services . BuildServiceProvider ( ) ;
87
+ AutoDiscoverResources ( _serviceDiscoveryFacade ) ;
88
+ AddResourcesFromDbContext ( dbContextType , intermediateProvider , _resourceGraphBuilder ) ;
89
+ UserConfigureResources ( configureResources , _resourceGraphBuilder ) ;
90
+ _services . AddSingleton ( _resourceGraphBuilder . Build ( ) ) ;
91
+ _intermediateServiceProviders . Add ( intermediateProvider ) ;
92
+ }
93
+
59
94
/// <summary>
60
95
/// Configures built-in .NET Core MVC (things like middleware, routing). Most of this configuration can be adjusted for the developers' need.
61
96
/// Before calling .AddJsonApi(), a developer can register their own implementation of the following services to customize startup:
@@ -92,32 +127,13 @@ public void ConfigureMvc()
92
127
_mvcBuilder . AddDataAnnotations ( ) ;
93
128
}
94
129
}
95
-
130
+
96
131
/// <summary>
97
- /// Configures and build the resource graph with resources from the provided sources and adds it to the DI container.
132
+ /// Discovers DI registrable services in the assemblies marked for discovery.
98
133
/// </summary>
99
- public void AddResourceGraph ( Type dbContextType , Action < IResourceGraphBuilder > configureResources )
100
- {
101
- var intermediateProvider = _services . BuildServiceProvider ( ) ;
102
- AutoDiscoverResources ( _serviceDiscoveryFacade ) ;
103
- AddResourcesFromDbContext ( dbContextType , intermediateProvider , _resourceGraphBuilder ) ;
104
- UserConfigureResources ( configureResources , _resourceGraphBuilder ) ;
105
- _services . AddSingleton ( _resourceGraphBuilder . Build ( ) ) ;
106
- _intermediateServiceProviders . Add ( intermediateProvider ) ;
107
- }
108
-
109
- public void ConfigureAutoDiscovery ( Action < IServiceDiscoveryFacade > configureAutoDiscovery )
110
- {
111
- var intermediateProvider = _services . BuildServiceProvider ( ) ;
112
- _serviceDiscoveryFacade = intermediateProvider . GetRequiredService < IServiceDiscoveryFacade > ( ) ;
113
- _resourceGraphBuilder = intermediateProvider . GetRequiredService < IResourceGraphBuilder > ( ) ;
114
- RegisterDiscoverableAssemblies ( configureAutoDiscovery , _serviceDiscoveryFacade ) ;
115
- _intermediateServiceProviders . Add ( intermediateProvider ) ;
116
- }
117
-
118
- private void RegisterDiscoverableAssemblies ( Action < IServiceDiscoveryFacade > configureAutoDiscovery , IServiceDiscoveryFacade serviceDiscoveryFacade )
134
+ public void DiscoverInjectables ( )
119
135
{
120
- configureAutoDiscovery ? . Invoke ( serviceDiscoveryFacade ) ;
136
+ _serviceDiscoveryFacade . DiscoverInjectables ( ) ;
121
137
}
122
138
123
139
/// <summary>
@@ -166,15 +182,20 @@ public void ConfigureServices(Type dbContextType)
166
182
167
183
_services . AddScoped < IInverseRelationships , InverseRelationships > ( ) ;
168
184
}
169
-
170
- /// <summary>
171
- /// Discovers DI registrable services in the assemblies marked for discovery.
172
- /// </summary>
173
- public void DiscoverInjectables ( )
185
+
186
+ public void DisposeIntermediateProviders ( )
174
187
{
175
- _serviceDiscoveryFacade . DiscoverInjectables ( ) ;
188
+ foreach ( var sp in _intermediateServiceProviders )
189
+ {
190
+ sp . Dispose ( ) ;
191
+ }
176
192
}
177
-
193
+
194
+ private void RegisterDiscoverableAssemblies ( Action < IServiceDiscoveryFacade > configureAutoDiscovery , IServiceDiscoveryFacade serviceDiscoveryFacade )
195
+ {
196
+ configureAutoDiscovery ? . Invoke ( serviceDiscoveryFacade ) ;
197
+ }
198
+
178
199
private void AddRepositoryLayer ( )
179
200
{
180
201
_services . AddScoped ( typeof ( IResourceRepository < > ) , typeof ( EntityFrameworkCoreRepository < > ) ) ;
@@ -245,14 +266,6 @@ private void AddQueryStringParameterServices()
245
266
_services . AddScoped < IQueryStringReader , QueryStringReader > ( ) ;
246
267
_services . AddSingleton < IRequestQueryStringAccessor , RequestQueryStringAccessor > ( ) ;
247
268
}
248
-
249
- public void DisposeIntermediateProviders ( )
250
- {
251
- foreach ( var sp in _intermediateServiceProviders )
252
- {
253
- sp . Dispose ( ) ;
254
- }
255
- }
256
269
257
270
private void AddResourceHooks ( )
258
271
{
@@ -275,19 +288,6 @@ private void AddServerSerialization()
275
288
_services . AddScoped ( sp => sp . GetRequiredService < IJsonApiSerializerFactory > ( ) . GetSerializer ( ) ) ;
276
289
_services . AddScoped < IResourceObjectBuilder , ResponseResourceObjectBuilder > ( ) ;
277
290
}
278
-
279
- /// <summary>
280
- /// Registers services that are required for the configuration of JsonApiDotNetCore during the start up.
281
- /// </summary>
282
- public void RegisterJsonApiStartupServices ( )
283
- {
284
- _services . AddSingleton < IJsonApiOptions > ( _options ) ;
285
- _services . TryAddSingleton < IJsonApiRoutingConvention , JsonApiRoutingConvention > ( ) ;
286
- _services . TryAddSingleton < IResourceGraphBuilder , ResourceGraphBuilder > ( ) ;
287
- _services . TryAddSingleton < IServiceDiscoveryFacade > ( sp => new ServiceDiscoveryFacade ( _services , sp . GetRequiredService < IResourceGraphBuilder > ( ) ) ) ;
288
- _services . TryAddScoped < IJsonApiExceptionFilterProvider , JsonApiExceptionFilterProvider > ( ) ;
289
- _services . TryAddScoped < IJsonApiTypeMatchFilterProvider , JsonApiTypeMatchFilterProvider > ( ) ;
290
- }
291
291
292
292
private void AddResourcesFromDbContext ( Type dbContextType , ServiceProvider intermediateProvider , IResourceGraphBuilder builder )
293
293
{
0 commit comments