Skip to content

Commit 4b859a3

Browse files
committed
hack(service-collection-ext): inject bogus dbContext types for DI
as far as I can tell, .Net's native DI will check all pathways for resolution even if they shouldn't be resolved. Specifically, the Internal.Generics classes require DbContext to be injected. However, they would never be resolved if the app does not actually use EF and implements their own IResourceService
1 parent b89b18f commit 4b859a3

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/JsonApiDotNetCore/Builders/ContextGraphBuilder.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace JsonApiDotNetCore.Builders
1010
public class ContextGraphBuilder : IContextGraphBuilder
1111
{
1212
private List<ContextEntity> Entities;
13-
13+
private bool _usesDbContext;
1414
public ContextGraphBuilder()
1515
{
1616
Entities = new List<ContextEntity>();
@@ -20,7 +20,8 @@ public IContextGraph Build()
2020
{
2121
var graph = new ContextGraph()
2222
{
23-
Entities = Entities
23+
Entities = Entities,
24+
UsesDbContext = _usesDbContext
2425
};
2526

2627
return graph;
@@ -81,6 +82,8 @@ protected virtual Type GetRelationshipType(RelationshipAttribute relation, Prope
8182

8283
public void AddDbContext<T>() where T : DbContext
8384
{
85+
_usesDbContext = true;
86+
8487
var contextType = typeof(T);
8588

8689
var entities = new List<ContextEntity>();

src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public static void AddJsonApi(this IServiceCollection services,
5555

5656
options(config);
5757

58+
mvcBuilder
59+
.AddMvcOptions(opt =>
60+
{
61+
opt.Filters.Add(typeof(JsonApiExceptionFilter));
62+
opt.SerializeAsJsonApi(config);
63+
});
64+
5865
AddJsonApiInternals(services, config);
5966
}
6067

@@ -73,6 +80,12 @@ public static void AddJsonApiInternals(
7380
this IServiceCollection services,
7481
JsonApiOptions jsonApiOptions)
7582
{
83+
if(!jsonApiOptions.ContextGraph.UsesDbContext)
84+
{
85+
services.AddScoped<DbContext>();
86+
services.AddSingleton<DbContextOptions>(new DbContextOptionsBuilder().Options);
87+
}
88+
7689
services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>));
7790
services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>));
7891
services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>));

src/JsonApiDotNetCore/Internal/ContextGraph.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace JsonApiDotNetCore.Internal
88
public class ContextGraph : IContextGraph
99
{
1010
public List<ContextEntity> Entities { get; set; }
11+
public bool UsesDbContext { get; set; }
1112

1213
public ContextEntity GetContextEntity(string dbSetName)
1314
{

src/JsonApiDotNetCore/Internal/IContextGraph.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public interface IContextGraph
88
string GetRelationshipName<TParent>(string relationshipName);
99
ContextEntity GetContextEntity(string dbSetName);
1010
ContextEntity GetContextEntity(Type entityType);
11+
bool UsesDbContext { get; set; }
1112
}
1213
}

0 commit comments

Comments
 (0)