Skip to content

Commit b89b18f

Browse files
committed
refactor(service-collection-ext): prevent breaking changes
Since AddJsonApiInternals<T> was previously exposed, it needs to remain. Also, this is where the DbContext service should be added to the service collection since users could call this instead of UseJsonApi. It would be unreasonable to expect users to inject their dbContexts as the DbContext base type
1 parent ba4719d commit b89b18f

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,62 @@ namespace JsonApiDotNetCore.Extensions
1515
{
1616
public static class IServiceCollectionExtensions
1717
{
18-
public static void AddJsonApi<TContext>(this IServiceCollection services)
18+
public static void AddJsonApi<TContext>(this IServiceCollection services)
1919
where TContext : DbContext
2020
{
2121
var mvcBuilder = services.AddMvc();
22-
AddJsonApi<TContext>(services, (opt) => {}, mvcBuilder);
22+
AddJsonApi<TContext>(services, (opt) => { }, mvcBuilder);
2323
}
2424

25-
public static void AddJsonApi<TContext>(this IServiceCollection services, Action<JsonApiOptions> options)
25+
public static void AddJsonApi<TContext>(this IServiceCollection services, Action<JsonApiOptions> options)
2626
where TContext : DbContext
2727
{
2828
var mvcBuilder = services.AddMvc();
2929
AddJsonApi<TContext>(services, options, mvcBuilder);
3030
}
3131

32-
public static void AddJsonApi<TContext>(this IServiceCollection services,
33-
Action<JsonApiOptions> options,
34-
IMvcBuilder mvcBuilder) where TContext : DbContext
32+
public static void AddJsonApi<TContext>(this IServiceCollection services,
33+
Action<JsonApiOptions> options,
34+
IMvcBuilder mvcBuilder) where TContext : DbContext
3535
{
3636
var config = new JsonApiOptions();
37-
38-
options(config);
3937

40-
if(config.ContextGraph == null)
41-
config.BuildContextGraph<TContext>(null);
42-
43-
services.AddScoped(typeof(DbContext), typeof(TContext));
38+
options(config);
4439

4540
mvcBuilder
46-
.AddMvcOptions(opt => {
41+
.AddMvcOptions(opt =>
42+
{
4743
opt.Filters.Add(typeof(JsonApiExceptionFilter));
4844
opt.SerializeAsJsonApi(config);
4945
});
5046

51-
AddJsonApiInternals(services, config);
47+
AddJsonApiInternals<TContext>(services, config);
5248
}
5349

54-
public static void AddJsonApi(this IServiceCollection services,
50+
public static void AddJsonApi(this IServiceCollection services,
5551
Action<JsonApiOptions> options,
5652
IMvcBuilder mvcBuilder)
5753
{
5854
var config = new JsonApiOptions();
59-
55+
6056
options(config);
6157

6258
AddJsonApiInternals(services, config);
6359
}
6460

61+
public static void AddJsonApiInternals<TContext>(
62+
this IServiceCollection services,
63+
JsonApiOptions jsonApiOptions) where TContext : DbContext
64+
{
65+
if (jsonApiOptions.ContextGraph == null)
66+
jsonApiOptions.BuildContextGraph<TContext>(null);
67+
68+
services.AddScoped(typeof(DbContext), typeof(TContext));
69+
AddJsonApiInternals(services, jsonApiOptions);
70+
}
71+
6572
public static void AddJsonApiInternals(
66-
this IServiceCollection services,
73+
this IServiceCollection services,
6774
JsonApiOptions jsonApiOptions)
6875
{
6976
services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>));
@@ -72,7 +79,7 @@ public static void AddJsonApiInternals(
7279
services.AddScoped(typeof(IResourceService<,>), typeof(EntityResourceService<,>));
7380
services.AddSingleton<JsonApiOptions>(jsonApiOptions);
7481
services.AddSingleton<IContextGraph>(jsonApiOptions.ContextGraph);
75-
services.AddScoped<IJsonApiContext,JsonApiContext>();
82+
services.AddScoped<IJsonApiContext, JsonApiContext>();
7683
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
7784
services.AddScoped<JsonApiRouteHandler>();
7885
services.AddScoped<IMetaBuilder, MetaBuilder>();
@@ -88,7 +95,7 @@ public static void AddJsonApiInternals(
8895
public static void SerializeAsJsonApi(this MvcOptions options, JsonApiOptions jsonApiOptions)
8996
{
9097
options.InputFormatters.Insert(0, new JsonApiInputFormatter());
91-
98+
9299
options.OutputFormatters.Insert(0, new JsonApiOutputFormatter());
93100

94101
options.Conventions.Insert(0, new DasherizedRoutingConvention(jsonApiOptions.Namespace));

0 commit comments

Comments
 (0)