From da2ec944980a1a31ab2be1e6045e75dee1c5731a Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Tue, 19 Dec 2017 06:17:57 -0600 Subject: [PATCH 1/6] chore(csproj): upgrade xunit and include dotnet watch --- build/dependencies.props | 2 +- .../JsonApiDotNetCoreExampleTests.csproj | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) 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/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 @@ - + + + + + From 3f7469005fdce9fd5bd28d314816dc21a4eb9c71 Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Tue, 19 Dec 2017 06:18:49 -0600 Subject: [PATCH 2/6] feat(DbContextReolver): make the implementation generic and remove injection of DbContext --- .../Data/DbContextResolver.cs | 8 ++++---- .../IServiceCollectionExtensions.cs | 4 ++-- .../Internal/Generics/GenericProcessor.cs | 5 +++-- .../Generics/GenericProcessorFactory.cs | 6 ++++-- .../IServiceCollectionExtensionsTests.cs | 20 +++++++++---------- 5 files changed, 23 insertions(+), 20 deletions(-) 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/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..6918976590 100644 --- a/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs +++ b/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs @@ -1,4 +1,5 @@ using System; +using JsonApiDotNetCore.Data; using Microsoft.EntityFrameworkCore; namespace JsonApiDotNetCore.Internal.Generics @@ -8,10 +9,11 @@ public class GenericProcessorFactory : IGenericProcessorFactory private readonly DbContext _dbContext; private readonly IServiceProvider _serviceProvider; - public GenericProcessorFactory(DbContext dbContext, + public GenericProcessorFactory( + IDbContextResolver dbContextResolver, IServiceProvider serviceProvider) { - _dbContext = dbContext; + _dbContext = dbContextResolver.GetContext(); _serviceProvider = serviceProvider; } 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()); From 9827f64cedf3ba5440b45a7374c9f42a3bf20119 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 19 Dec 2017 12:51:51 -0600 Subject: [PATCH 3/6] feat(*): remove non-EF dependencies on DbContextResolver --- src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs | 9 +++++---- .../Internal/Generics/GenericProcessorFactory.cs | 8 +------- src/JsonApiDotNetCore/Services/IJsonApiContext.cs | 3 --- src/JsonApiDotNetCore/Services/JsonApiContext.cs | 6 ------ .../Repositories/AuthorizedTodoItemsRepository.cs | 3 ++- 5 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs b/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs index 1199389c60..7e7f4345f4 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) { } } @@ -50,9 +51,9 @@ public DefaultEntityRepository( public DefaultEntityRepository( ILoggerFactory loggerFactory, - IJsonApiContext jsonApiContext) + IJsonApiContext jsonApiContext, + IDbContextResolver contextResolver) { - var contextResolver = jsonApiContext.GetDbContextResolver(); _context = contextResolver.GetContext(); _dbSet = contextResolver.GetDbSet(); _jsonApiContext = jsonApiContext; diff --git a/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs b/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs index 6918976590..4b4e76525c 100644 --- a/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs +++ b/src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs @@ -1,19 +1,13 @@ using System; -using JsonApiDotNetCore.Data; -using Microsoft.EntityFrameworkCore; namespace JsonApiDotNetCore.Internal.Generics { public class GenericProcessorFactory : IGenericProcessorFactory { - private readonly DbContext _dbContext; private readonly IServiceProvider _serviceProvider; - public GenericProcessorFactory( - IDbContextResolver dbContextResolver, - IServiceProvider serviceProvider) + public GenericProcessorFactory(IServiceProvider serviceProvider) { - _dbContext = dbContextResolver.GetContext(); _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; From c9034e8fe2fa627450ec9ad10de9c145ccddea35 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 19 Dec 2017 12:52:15 -0600 Subject: [PATCH 4/6] ci(Build.ps1): check exit codes --- Build.ps1 | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) 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 From 5e3cf17c02eea0000be7445907d10fc2889fb2bd Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 19 Dec 2017 12:54:36 -0600 Subject: [PATCH 5/6] tests(DefaultEntityRepository): fix test setup --- test/UnitTests/Data/DefaultEntityRepository_Tests.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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); } } } From 313a08588272540130f2683ea11868ae02cb7b94 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 19 Dec 2017 13:13:42 -0600 Subject: [PATCH 6/6] chore(DefaultEntityRepository): drop obsolete constructor --- .../Data/DefaultEntityRepository.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs b/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs index 7e7f4345f4..5b5b0d1a55 100644 --- a/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs +++ b/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs @@ -36,19 +36,6 @@ 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) - { - _context = context; - _dbSet = context.GetDbSet(); - _jsonApiContext = jsonApiContext; - _logger = loggerFactory.CreateLogger>(); - _genericProcessorFactory = _jsonApiContext.GenericProcessorFactory; - } - public DefaultEntityRepository( ILoggerFactory loggerFactory, IJsonApiContext jsonApiContext,