diff --git a/Build.ps1 b/Build.ps1
index 8b4e2cf44c..e51473adf2 100644
--- a/Build.ps1
+++ b/Build.ps1
@@ -9,34 +9,52 @@ function Get-Version-Suffix-From-Tag
return $final
}
+function CheckLastExitCode {
+ param ([int[]]$SuccessCodes = @(0), [scriptblock]$CleanupScript=$null)
+
+ if ($SuccessCodes -notcontains $LastExitCode) {
+ $msg = "EXE RETURNED EXIT CODE $LastExitCode"
+ throw $msg
+ }
+}
+
$revision = @{ $true = $env:APPVEYOR_BUILD_NUMBER; $false = 1 }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
$revision = "{0:D4}" -f [convert]::ToInt32($revision, 10)
dotnet restore
dotnet test ./test/UnitTests/UnitTests.csproj
+CheckLastExitCode
+
dotnet test ./test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj
+CheckLastExitCode
+
dotnet test ./test/NoEntityFrameworkTests/NoEntityFrameworkTests.csproj
+CheckLastExitCode
dotnet build .\src\JsonApiDotNetCore -c Release
+CheckLastExitCode
-echo "APPVEYOR_REPO_TAG: $env:APPVEYOR_REPO_TAG"
+Write-Output "APPVEYOR_REPO_TAG: $env:APPVEYOR_REPO_TAG"
If($env:APPVEYOR_REPO_TAG -eq $true) {
$revision = Get-Version-Suffix-From-Tag
- echo "VERSION-SUFFIX: $revision"
+ Write-Output "VERSION-SUFFIX: $revision"
IF ([string]::IsNullOrWhitespace($revision)){
- echo "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts"
+ Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts
+ CheckLastExitCode
}
Else {
- echo "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision"
+ Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision
+ CheckLastExitCode
}
}
Else {
- echo "VERSION-SUFFIX: alpha1-$revision"
- echo "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=alpha1-$revision"
+ Write-Output "VERSION-SUFFIX: alpha1-$revision"
+ Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=alpha1-$revision"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=alpha1-$revision
+ CheckLastExitCode
}
\ No newline at end of file
diff --git a/build/dependencies.props b/build/dependencies.props
index 13b9ba4ecb..0ef1b750a7 100644
--- a/build/dependencies.props
+++ b/build/dependencies.props
@@ -5,7 +5,7 @@
4.7.10
- 2.2.0
+ 2.3.1
8.0.1-beta-1
diff --git a/src/JsonApiDotNetCore/Data/DbContextResolver.cs b/src/JsonApiDotNetCore/Data/DbContextResolver.cs
index 8aedee0f4e..7cfe0a4278 100644
--- a/src/JsonApiDotNetCore/Data/DbContextResolver.cs
+++ b/src/JsonApiDotNetCore/Data/DbContextResolver.cs
@@ -1,14 +1,14 @@
-using System;
using JsonApiDotNetCore.Extensions;
using Microsoft.EntityFrameworkCore;
namespace JsonApiDotNetCore.Data
{
- public class DbContextResolver : IDbContextResolver
+ public class DbContextResolver : IDbContextResolver
+ where TContext : DbContext
{
- private readonly DbContext _context;
+ private readonly TContext _context;
- public DbContextResolver(DbContext context)
+ public DbContextResolver(TContext context)
{
_context = context;
}
diff --git a/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs b/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
index 1199389c60..5b5b0d1a55 100644
--- a/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
+++ b/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
@@ -20,8 +20,9 @@ public class DefaultEntityRepository
{
public DefaultEntityRepository(
ILoggerFactory loggerFactory,
- IJsonApiContext jsonApiContext)
- : base(loggerFactory, jsonApiContext)
+ IJsonApiContext jsonApiContext,
+ IDbContextResolver contextResolver)
+ : base(loggerFactory, jsonApiContext, contextResolver)
{ }
}
@@ -35,24 +36,11 @@ public class DefaultEntityRepository
private readonly IJsonApiContext _jsonApiContext;
private readonly IGenericProcessorFactory _genericProcessorFactory;
- [Obsolete("DbContext is no longer directly injected into the ctor. Use JsonApiContext.GetDbContextResolver() instead")]
public DefaultEntityRepository(
- DbContext context,
ILoggerFactory loggerFactory,
- IJsonApiContext jsonApiContext)
+ IJsonApiContext jsonApiContext,
+ IDbContextResolver contextResolver)
{
- _context = context;
- _dbSet = context.GetDbSet();
- _jsonApiContext = jsonApiContext;
- _logger = loggerFactory.CreateLogger>();
- _genericProcessorFactory = _jsonApiContext.GenericProcessorFactory;
- }
-
- public DefaultEntityRepository(
- ILoggerFactory loggerFactory,
- IJsonApiContext jsonApiContext)
- {
- var contextResolver = jsonApiContext.GetDbContextResolver();
_context = contextResolver.GetContext();
_dbSet = contextResolver.GetDbSet();
_jsonApiContext = jsonApiContext;
diff --git a/src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs b/src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs
index afcc11ee8d..1549e77e0f 100644
--- a/src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs
+++ b/src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs
@@ -77,7 +77,8 @@ public static void AddJsonApiInternals(
if (jsonApiOptions.ContextGraph == null)
jsonApiOptions.BuildContextGraph(null);
- services.AddScoped(typeof(DbContext), typeof(TContext));
+ services.AddScoped>();
+
AddJsonApiInternals(services, jsonApiOptions);
}
@@ -91,7 +92,6 @@ public static void AddJsonApiInternals(
services.AddSingleton(new DbContextOptionsBuilder().Options);
}
- services.AddScoped();
services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>));
services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>));
services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>));
diff --git a/src/JsonApiDotNetCore/Internal/Generics/GenericProcessor.cs b/src/JsonApiDotNetCore/Internal/Generics/GenericProcessor.cs
index ce76b47dae..781f8ae7f3 100644
--- a/src/JsonApiDotNetCore/Internal/Generics/GenericProcessor.cs
+++ b/src/JsonApiDotNetCore/Internal/Generics/GenericProcessor.cs
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
+using JsonApiDotNetCore.Data;
using JsonApiDotNetCore.Extensions;
using JsonApiDotNetCore.Models;
using Microsoft.EntityFrameworkCore;
@@ -10,9 +11,9 @@ namespace JsonApiDotNetCore.Internal.Generics
public class GenericProcessor : IGenericProcessor where T : class, IIdentifiable
{
private readonly DbContext _context;
- public GenericProcessor(DbContext context)
+ public GenericProcessor(IDbContextResolver contextResolver)
{
- _context = context;
+ _context = contextResolver.GetContext();
}
public async Task UpdateRelationshipsAsync(object parent, RelationshipAttribute relationship, IEnumerable relationshipIds)
diff --git a/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs b/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs
index a238e4ef9f..4b4e76525c 100644
--- a/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs
+++ b/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs
@@ -1,17 +1,13 @@
using System;
-using Microsoft.EntityFrameworkCore;
namespace JsonApiDotNetCore.Internal.Generics
{
public class GenericProcessorFactory : IGenericProcessorFactory
{
- private readonly DbContext _dbContext;
private readonly IServiceProvider _serviceProvider;
- public GenericProcessorFactory(DbContext dbContext,
- IServiceProvider serviceProvider)
+ public GenericProcessorFactory(IServiceProvider serviceProvider)
{
- _dbContext = dbContext;
_serviceProvider = serviceProvider;
}
diff --git a/src/JsonApiDotNetCore/Services/IJsonApiContext.cs b/src/JsonApiDotNetCore/Services/IJsonApiContext.cs
index 7b1b9e67a8..ee7ee10a35 100644
--- a/src/JsonApiDotNetCore/Services/IJsonApiContext.cs
+++ b/src/JsonApiDotNetCore/Services/IJsonApiContext.cs
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
-using System.Reflection;
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Configuration;
-using JsonApiDotNetCore.Data;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Internal.Generics;
using JsonApiDotNetCore.Internal.Query;
@@ -29,6 +27,5 @@ public interface IJsonApiContext
Dictionary RelationshipsToUpdate { get; set; }
Type ControllerType { get; set; }
TAttribute GetControllerAttribute() where TAttribute : Attribute;
- IDbContextResolver GetDbContextResolver();
}
}
diff --git a/src/JsonApiDotNetCore/Services/JsonApiContext.cs b/src/JsonApiDotNetCore/Services/JsonApiContext.cs
index b223e47bf5..5304d77b29 100644
--- a/src/JsonApiDotNetCore/Services/JsonApiContext.cs
+++ b/src/JsonApiDotNetCore/Services/JsonApiContext.cs
@@ -3,7 +3,6 @@
using System.Linq;
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Configuration;
-using JsonApiDotNetCore.Data;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Internal.Generics;
using JsonApiDotNetCore.Internal.Query;
@@ -15,12 +14,10 @@ namespace JsonApiDotNetCore.Services
public class JsonApiContext : IJsonApiContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
- private readonly IDbContextResolver _contextResolver;
private readonly IQueryParser _queryParser;
private readonly IControllerContext _controllerContext;
public JsonApiContext(
- IDbContextResolver contextResolver,
IContextGraph contextGraph,
IHttpContextAccessor httpContextAccessor,
JsonApiOptions options,
@@ -29,7 +26,6 @@ public JsonApiContext(
IQueryParser queryParser,
IControllerContext controllerContext)
{
- _contextResolver = contextResolver;
ContextGraph = contextGraph;
_httpContextAccessor = httpContextAccessor;
Options = options;
@@ -81,8 +77,6 @@ public IJsonApiContext ApplyContext(object controller)
return this;
}
- public IDbContextResolver GetDbContextResolver() => _contextResolver;
-
private PageManager GetPageManager()
{
if (Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0))
diff --git a/test/JsonApiDotNetCoreExampleTests/Helpers/Repositories/AuthorizedTodoItemsRepository.cs b/test/JsonApiDotNetCoreExampleTests/Helpers/Repositories/AuthorizedTodoItemsRepository.cs
index 7e9abf3784..32cb24cdcf 100644
--- a/test/JsonApiDotNetCoreExampleTests/Helpers/Repositories/AuthorizedTodoItemsRepository.cs
+++ b/test/JsonApiDotNetCoreExampleTests/Helpers/Repositories/AuthorizedTodoItemsRepository.cs
@@ -16,8 +16,9 @@ public class AuthorizedTodoItemsRepository : DefaultEntityRepository
public AuthorizedTodoItemsRepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext,
+ IDbContextResolver contextResolver,
IAuthorizationService authService)
- : base(loggerFactory, jsonApiContext)
+ : base(loggerFactory, jsonApiContext, contextResolver)
{
_logger = loggerFactory.CreateLogger();
_authService = authService;
diff --git a/test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj b/test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj
index 1b40e2dd73..43500cb84b 100755
--- a/test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj
+++ b/test/JsonApiDotNetCoreExampleTests/JsonApiDotNetCoreExampleTests.csproj
@@ -30,5 +30,9 @@
-
+
+
+
+
+
diff --git a/test/JsonApiDotNetCoreExampleTests/Unit/Extensions/IServiceCollectionExtensionsTests.cs b/test/JsonApiDotNetCoreExampleTests/Unit/Extensions/IServiceCollectionExtensionsTests.cs
index 95b05df096..5d68306493 100644
--- a/test/JsonApiDotNetCoreExampleTests/Unit/Extensions/IServiceCollectionExtensionsTests.cs
+++ b/test/JsonApiDotNetCoreExampleTests/Unit/Extensions/IServiceCollectionExtensionsTests.cs
@@ -1,19 +1,19 @@
-using Xunit;
using JsonApiDotNetCore.Builders;
-using Microsoft.Extensions.DependencyInjection;
-using JsonApiDotNetCore.Extensions;
using JsonApiDotNetCore.Configuration;
-using Microsoft.EntityFrameworkCore;
using JsonApiDotNetCore.Data;
+using JsonApiDotNetCore.Extensions;
+using JsonApiDotNetCore.Formatters;
using JsonApiDotNetCore.Internal;
-using Microsoft.AspNetCore.Http;
+using JsonApiDotNetCore.Internal.Generics;
+using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Data;
-using Microsoft.Extensions.Caching.Memory;
using JsonApiDotNetCoreExample.Models;
-using JsonApiDotNetCore.Serialization;
-using JsonApiDotNetCore.Formatters;
-using JsonApiDotNetCore.Internal.Generics;
+using Microsoft.AspNetCore.Http;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.DependencyInjection;
+using Xunit;
namespace JsonApiDotNetCoreExampleTests.Unit.Extensions
{
@@ -36,7 +36,7 @@ public void AddJsonApiInternals_Adds_All_Required_Services()
var provider = services.BuildServiceProvider();
// assert
- Assert.NotNull(provider.GetService());
+ Assert.NotNull(provider.GetService());
Assert.NotNull(provider.GetService(typeof(IEntityRepository)));
Assert.NotNull(provider.GetService());
Assert.NotNull(provider.GetService());
diff --git a/test/UnitTests/Data/DefaultEntityRepository_Tests.cs b/test/UnitTests/Data/DefaultEntityRepository_Tests.cs
index e596b35317..50d24409f8 100644
--- a/test/UnitTests/Data/DefaultEntityRepository_Tests.cs
+++ b/test/UnitTests/Data/DefaultEntityRepository_Tests.cs
@@ -89,13 +89,10 @@ private DefaultEntityRepository GetRepository()
.Setup(m => m.RelationshipsToUpdate)
.Returns(_relationshipsToUpdate);
- _jsonApiContextMock
- .Setup(m => m.GetDbContextResolver())
- .Returns(_contextResolverMock.Object);
-
return new DefaultEntityRepository(
_loggFactoryMock.Object,
- _jsonApiContextMock.Object);
+ _jsonApiContextMock.Object,
+ _contextResolverMock.Object);
}
}
}