@@ -29,52 +29,99 @@ namespace JsonApiDotNetCore.Extensions
29
29
// ReSharper disable once InconsistentNaming
30
30
public static class IServiceCollectionExtensions
31
31
{
32
- public static IServiceCollection AddJsonApi < TContext > ( this IServiceCollection services )
32
+ static private readonly Action < JsonApiOptions > _noopConfig = opt => { } ;
33
+ static private JsonApiOptions _options { get { return new JsonApiOptions ( ) ; } }
34
+ public static IServiceCollection AddJsonApi < TContext > ( this IServiceCollection services ,
35
+ IMvcCoreBuilder mvcBuilder = null )
33
36
where TContext : DbContext
34
37
{
35
- var mvcBuilder = services . AddMvcCore ( ) ;
36
- return AddJsonApi < TContext > ( services , opt => { } , mvcBuilder ) ;
38
+ return AddJsonApi < TContext > ( services , _noopConfig , mvcBuilder ) ;
37
39
}
38
40
39
- public static IServiceCollection AddJsonApi < TContext > ( this IServiceCollection services , Action < JsonApiOptions > options )
41
+ /// <summary>
42
+ /// Enabling JsonApiDotNetCore using the EF Core DbContext to build the ResourceGraph.
43
+ /// </summary>
44
+ /// <typeparam name="TContext"></typeparam>
45
+ /// <param name="services"></param>
46
+ /// <param name="configureAction"></param>
47
+ /// <returns></returns>
48
+ public static IServiceCollection AddJsonApi < TContext > ( this IServiceCollection services ,
49
+ Action < JsonApiOptions > configureAction ,
50
+ IMvcCoreBuilder mvcBuilder = null )
40
51
where TContext : DbContext
41
52
{
42
- var mvcBuilder = services . AddMvcCore ( ) ;
43
- return AddJsonApi < TContext > ( services , options , mvcBuilder ) ;
53
+ var options = _options ;
54
+ // add basic Mvc functionality
55
+ mvcBuilder = mvcBuilder ?? services . AddMvcCore ( ) ;
56
+ // set standard options
57
+ configureAction ( options ) ;
58
+
59
+ // ResourceGraphBuilder should not be exposed on JsonApiOptions.
60
+ // Instead, ResourceGraphBuilder should consume JsonApiOptions
61
+
62
+ // build the resource graph using ef core DbContext
63
+ options . BuildResourceGraph ( builder => builder . AddDbContext < TContext > ( ) ) ;
64
+
65
+ // add JsonApi fitlers and serializer
66
+ mvcBuilder . AddMvcOptions ( opt => AddMvcOptions ( opt , options ) ) ;
67
+
68
+ // register services
69
+ AddJsonApiInternals < TContext > ( services , options ) ;
70
+ return services ;
44
71
}
45
72
46
- public static IServiceCollection AddJsonApi < TContext > (
47
- this IServiceCollection services ,
48
- Action < JsonApiOptions > options ,
49
- IMvcCoreBuilder mvcBuilder ) where TContext : DbContext
73
+
74
+ /// <summary>
75
+ /// Enabling JsonApiDotNetCore using manual declaration to build the ResourceGraph.
76
+ /// </summary>
77
+ /// <param name="services"></param>
78
+ /// <param name="configureOptions"></param>
79
+ /// <returns></returns>
80
+ public static IServiceCollection AddJsonApi ( this IServiceCollection services ,
81
+ Action < JsonApiOptions > configureOptions ,
82
+ IMvcCoreBuilder mvcBuilder = null )
50
83
{
51
- var config = new JsonApiOptions ( ) ;
52
- options ( config ) ;
53
- config . BuildResourceGraph ( builder => builder . AddDbContext < TContext > ( ) ) ;
54
- mvcBuilder . AddMvcOptions ( opt => AddMvcOptions ( opt , config ) ) ;
55
- AddJsonApiInternals < TContext > ( services , config ) ;
84
+ var options = _options ;
85
+ mvcBuilder = mvcBuilder ?? services . AddMvcCore ( ) ;
86
+ configureOptions ( options ) ;
87
+
88
+ // add JsonApi fitlers and serializer
89
+ mvcBuilder . AddMvcOptions ( opt => AddMvcOptions ( opt , options ) ) ;
90
+
91
+ // register services
92
+ AddJsonApiInternals ( services , options ) ;
56
93
return services ;
57
94
}
58
95
59
- public static IServiceCollection AddJsonApi (
60
- this IServiceCollection services ,
61
- Action < JsonApiOptions > configureOptions ,
62
- IMvcCoreBuilder mvcBuilder ,
63
- Action < ServiceDiscoveryFacade > autoDiscover = null )
96
+ /// <summary>
97
+ /// Enabling JsonApiDotNetCore using the EF Core DbContext to build the ResourceGraph.
98
+ /// </summary>
99
+ /// <param name="services"></param>
100
+ /// <param name="configureOptions"></param>
101
+ /// <param name="autoDiscover"></param>
102
+ /// <returns></returns>
103
+ public static IServiceCollection AddJsonApi ( this IServiceCollection services ,
104
+ Action < JsonApiOptions > configureOptions ,
105
+ Action < ServiceDiscoveryFacade > autoDiscover ,
106
+ IMvcCoreBuilder mvcBuilder = null )
64
107
{
65
- var config = new JsonApiOptions ( ) ;
66
- configureOptions ( config ) ;
108
+ var options = _options ;
109
+ mvcBuilder = mvcBuilder ?? services . AddMvcCore ( ) ;
110
+ configureOptions ( options ) ;
67
111
68
- if ( autoDiscover != null )
69
- {
70
- var facade = new ServiceDiscoveryFacade ( services , config . ResourceGraphBuilder ) ;
71
- autoDiscover ( facade ) ;
72
- }
73
- mvcBuilder . AddMvcOptions ( opt => AddMvcOptions ( opt , config ) ) ;
74
- AddJsonApiInternals ( services , config ) ;
112
+ // build the resource graph using auto discovery.
113
+ var facade = new ServiceDiscoveryFacade ( services , options . ResourceGraphBuilder ) ;
114
+ autoDiscover ( facade ) ;
115
+
116
+ // add JsonApi fitlers and serializer
117
+ mvcBuilder . AddMvcOptions ( opt => AddMvcOptions ( opt , options ) ) ;
118
+
119
+ // register services
120
+ AddJsonApiInternals ( services , options ) ;
75
121
return services ;
76
122
}
77
123
124
+
78
125
private static void AddMvcOptions ( MvcOptions options , JsonApiOptions config )
79
126
{
80
127
options . Filters . Add ( typeof ( JsonApiExceptionFilter ) ) ;
@@ -144,7 +191,6 @@ public static void AddJsonApiInternals(
144
191
services . AddScoped ( typeof ( IResourceService < , > ) , typeof ( EntityResourceService < , > ) ) ;
145
192
146
193
services . AddScoped < ILinkBuilder , LinkBuilder > ( ) ;
147
- services . AddScoped < ITraversalHelper , TraversalHelper > ( ) ;
148
194
services . AddScoped < IRequestManager , RequestManager > ( ) ;
149
195
services . AddSingleton < IJsonApiOptions > ( jsonApiOptions ) ;
150
196
services . AddScoped < IPageManager , PageManager > ( ) ;
@@ -173,7 +219,7 @@ public static void AddJsonApiInternals(
173
219
services . AddTransient ( typeof ( IResourceHookExecutor ) , typeof ( ResourceHookExecutor ) ) ;
174
220
services . AddTransient < IHookExecutorHelper , HookExecutorHelper > ( ) ;
175
221
}
176
- services . AddTransient < IActionContextAccessor , ActionContextAccessor > ( ) ;
222
+ // services.AddTransient<IActionContextAccessor, ActionContextAccessor>();
177
223
178
224
services . AddScoped < IInverseRelationships , InverseRelationships > ( ) ;
179
225
}
0 commit comments