Skip to content

Commit 68fdee9

Browse files
committed
feat(DbContextReolver): make the implementation generic and remove injection of DbContext
1 parent 2c8f285 commit 68fdee9

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

src/JsonApiDotNetCore/Data/DbContextResolver.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System;
21
using JsonApiDotNetCore.Extensions;
32
using Microsoft.EntityFrameworkCore;
43

54
namespace JsonApiDotNetCore.Data
65
{
7-
public class DbContextResolver : IDbContextResolver
6+
public class DbContextResolver<TContext> : IDbContextResolver
7+
where TContext : DbContext
88
{
9-
private readonly DbContext _context;
9+
private readonly TContext _context;
1010

11-
public DbContextResolver(DbContext context)
11+
public DbContextResolver(TContext context)
1212
{
1313
_context = context;
1414
}

src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public static void AddJsonApiInternals<TContext>(
7777
if (jsonApiOptions.ContextGraph == null)
7878
jsonApiOptions.BuildContextGraph<TContext>(null);
7979

80-
services.AddScoped(typeof(DbContext), typeof(TContext));
80+
services.AddScoped<IDbContextResolver, DbContextResolver<TContext>>();
81+
8182
AddJsonApiInternals(services, jsonApiOptions);
8283
}
8384

@@ -91,7 +92,6 @@ public static void AddJsonApiInternals(
9192
services.AddSingleton<DbContextOptions>(new DbContextOptionsBuilder().Options);
9293
}
9394

94-
services.AddScoped<IDbContextResolver, DbContextResolver>();
9595
services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>));
9696
services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>));
9797
services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>));

src/JsonApiDotNetCore/Internal/Generics/GenericProcessor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Threading.Tasks;
4+
using JsonApiDotNetCore.Data;
45
using JsonApiDotNetCore.Extensions;
56
using JsonApiDotNetCore.Models;
67
using Microsoft.EntityFrameworkCore;
@@ -10,9 +11,9 @@ namespace JsonApiDotNetCore.Internal.Generics
1011
public class GenericProcessor<T> : IGenericProcessor where T : class, IIdentifiable
1112
{
1213
private readonly DbContext _context;
13-
public GenericProcessor(DbContext context)
14+
public GenericProcessor(IDbContextResolver contextResolver)
1415
{
15-
_context = context;
16+
_context = contextResolver.GetContext();
1617
}
1718

1819
public async Task UpdateRelationshipsAsync(object parent, RelationshipAttribute relationship, IEnumerable<string> relationshipIds)

src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using JsonApiDotNetCore.Data;
23
using Microsoft.EntityFrameworkCore;
34

45
namespace JsonApiDotNetCore.Internal.Generics
@@ -8,10 +9,11 @@ public class GenericProcessorFactory : IGenericProcessorFactory
89
private readonly DbContext _dbContext;
910
private readonly IServiceProvider _serviceProvider;
1011

11-
public GenericProcessorFactory(DbContext dbContext,
12+
public GenericProcessorFactory(
13+
IDbContextResolver dbContextResolver,
1214
IServiceProvider serviceProvider)
1315
{
14-
_dbContext = dbContext;
16+
_dbContext = dbContextResolver.GetContext();
1517
_serviceProvider = serviceProvider;
1618
}
1719

test/JsonApiDotNetCoreExampleTests/Unit/Extensions/IServiceCollectionExtensionsTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
using Xunit;
21
using JsonApiDotNetCore.Builders;
3-
using Microsoft.Extensions.DependencyInjection;
4-
using JsonApiDotNetCore.Extensions;
52
using JsonApiDotNetCore.Configuration;
6-
using Microsoft.EntityFrameworkCore;
73
using JsonApiDotNetCore.Data;
4+
using JsonApiDotNetCore.Extensions;
5+
using JsonApiDotNetCore.Formatters;
86
using JsonApiDotNetCore.Internal;
9-
using Microsoft.AspNetCore.Http;
7+
using JsonApiDotNetCore.Internal.Generics;
8+
using JsonApiDotNetCore.Serialization;
109
using JsonApiDotNetCore.Services;
1110
using JsonApiDotNetCoreExample.Data;
12-
using Microsoft.Extensions.Caching.Memory;
1311
using JsonApiDotNetCoreExample.Models;
14-
using JsonApiDotNetCore.Serialization;
15-
using JsonApiDotNetCore.Formatters;
16-
using JsonApiDotNetCore.Internal.Generics;
12+
using Microsoft.AspNetCore.Http;
13+
using Microsoft.EntityFrameworkCore;
14+
using Microsoft.Extensions.Caching.Memory;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Xunit;
1717

1818
namespace JsonApiDotNetCoreExampleTests.Unit.Extensions
1919
{
@@ -36,7 +36,7 @@ public void AddJsonApiInternals_Adds_All_Required_Services()
3636
var provider = services.BuildServiceProvider();
3737

3838
// assert
39-
Assert.NotNull(provider.GetService<DbContext>());
39+
Assert.NotNull(provider.GetService<IDbContextResolver>());
4040
Assert.NotNull(provider.GetService(typeof(IEntityRepository<TodoItem>)));
4141
Assert.NotNull(provider.GetService<JsonApiOptions>());
4242
Assert.NotNull(provider.GetService<IContextGraph>());

0 commit comments

Comments
 (0)