@@ -56,6 +56,41 @@ public void ConfigureJsonApiOptions(Action<JsonApiOptions> configureOptions)
5656 configureOptions ? . Invoke ( _options ) ;
5757 }
5858
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+
5994 /// <summary>
6095 /// Configures built-in .NET Core MVC (things like middleware, routing). Most of this configuration can be adjusted for the developers' need.
6196 /// Before calling .AddJsonApi(), a developer can register their own implementation of the following services to customize startup:
@@ -92,32 +127,13 @@ public void ConfigureMvc()
92127 _mvcBuilder . AddDataAnnotations ( ) ;
93128 }
94129 }
95-
130+
96131 /// <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.
98133 /// </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 ( )
119135 {
120- configureAutoDiscovery ? . Invoke ( serviceDiscoveryFacade ) ;
136+ _serviceDiscoveryFacade . DiscoverInjectables ( ) ;
121137 }
122138
123139 /// <summary>
@@ -166,15 +182,20 @@ public void ConfigureServices(Type dbContextType)
166182
167183 _services . AddScoped < IInverseRelationships , InverseRelationships > ( ) ;
168184 }
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 ( )
174187 {
175- _serviceDiscoveryFacade . DiscoverInjectables ( ) ;
188+ foreach ( var sp in _intermediateServiceProviders )
189+ {
190+ sp . Dispose ( ) ;
191+ }
176192 }
177-
193+
194+ private void RegisterDiscoverableAssemblies ( Action < IServiceDiscoveryFacade > configureAutoDiscovery , IServiceDiscoveryFacade serviceDiscoveryFacade )
195+ {
196+ configureAutoDiscovery ? . Invoke ( serviceDiscoveryFacade ) ;
197+ }
198+
178199 private void AddRepositoryLayer ( )
179200 {
180201 _services . AddScoped ( typeof ( IResourceRepository < > ) , typeof ( EntityFrameworkCoreRepository < > ) ) ;
@@ -245,14 +266,6 @@ private void AddQueryStringParameterServices()
245266 _services . AddScoped < IQueryStringReader , QueryStringReader > ( ) ;
246267 _services . AddSingleton < IRequestQueryStringAccessor , RequestQueryStringAccessor > ( ) ;
247268 }
248-
249- public void DisposeIntermediateProviders ( )
250- {
251- foreach ( var sp in _intermediateServiceProviders )
252- {
253- sp . Dispose ( ) ;
254- }
255- }
256269
257270 private void AddResourceHooks ( )
258271 {
@@ -275,19 +288,6 @@ private void AddServerSerialization()
275288 _services . AddScoped ( sp => sp . GetRequiredService < IJsonApiSerializerFactory > ( ) . GetSerializer ( ) ) ;
276289 _services . AddScoped < IResourceObjectBuilder , ResponseResourceObjectBuilder > ( ) ;
277290 }
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- }
291291
292292 private void AddResourcesFromDbContext ( Type dbContextType , ServiceProvider intermediateProvider , IResourceGraphBuilder builder )
293293 {
0 commit comments