Skip to content

Generic DbContextResolver #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>
<PropertyGroup>
<MoqVersion>4.7.10</MoqVersion>
<xUnitVersion>2.2.0</xUnitVersion>
<xUnitVersion>2.3.1</xUnitVersion>
<BogusVersion>8.0.1-beta-1</BogusVersion>
</PropertyGroup>
</Project>
8 changes: 4 additions & 4 deletions src/JsonApiDotNetCore/Data/DbContextResolver.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using JsonApiDotNetCore.Extensions;
using Microsoft.EntityFrameworkCore;

namespace JsonApiDotNetCore.Data
{
public class DbContextResolver : IDbContextResolver
public class DbContextResolver<TContext> : IDbContextResolver
where TContext : DbContext
{
private readonly DbContext _context;
private readonly TContext _context;

public DbContextResolver(DbContext context)
public DbContextResolver(TContext context)
{
_context = context;
}
Expand Down
22 changes: 5 additions & 17 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public class DefaultEntityRepository<TEntity>
{
public DefaultEntityRepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext)
: base(loggerFactory, jsonApiContext)
IJsonApiContext jsonApiContext,
IDbContextResolver contextResolver)
: base(loggerFactory, jsonApiContext, contextResolver)
{ }
}

Expand All @@ -35,24 +36,11 @@ public class DefaultEntityRepository<TEntity, TId>
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<TEntity>();
_jsonApiContext = jsonApiContext;
_logger = loggerFactory.CreateLogger<DefaultEntityRepository<TEntity, TId>>();
_genericProcessorFactory = _jsonApiContext.GenericProcessorFactory;
}

public DefaultEntityRepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext)
{
var contextResolver = jsonApiContext.GetDbContextResolver();
_context = contextResolver.GetContext();
_dbSet = contextResolver.GetDbSet<TEntity>();
_jsonApiContext = jsonApiContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public static void AddJsonApiInternals<TContext>(
if (jsonApiOptions.ContextGraph == null)
jsonApiOptions.BuildContextGraph<TContext>(null);

services.AddScoped(typeof(DbContext), typeof(TContext));
services.AddScoped<IDbContextResolver, DbContextResolver<TContext>>();

AddJsonApiInternals(services, jsonApiOptions);
}

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

services.AddScoped<IDbContextResolver, DbContextResolver>();
services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>));
services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>));
services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>));
Expand Down
5 changes: 3 additions & 2 deletions src/JsonApiDotNetCore/Internal/Generics/GenericProcessor.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,9 +11,9 @@ namespace JsonApiDotNetCore.Internal.Generics
public class GenericProcessor<T> : 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<string> relationshipIds)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
3 changes: 0 additions & 3 deletions src/JsonApiDotNetCore/Services/IJsonApiContext.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -29,6 +27,5 @@ public interface IJsonApiContext
Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; }
Type ControllerType { get; set; }
TAttribute GetControllerAttribute<TAttribute>() where TAttribute : Attribute;
IDbContextResolver GetDbContextResolver();
}
}
6 changes: 0 additions & 6 deletions src/JsonApiDotNetCore/Services/JsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -29,7 +26,6 @@ public JsonApiContext(
IQueryParser queryParser,
IControllerContext controllerContext)
{
_contextResolver = contextResolver;
ContextGraph = contextGraph;
_httpContextAccessor = httpContextAccessor;
Options = options;
Expand Down Expand Up @@ -81,8 +77,6 @@ public IJsonApiContext ApplyContext<T>(object controller)
return this;
}

public IDbContextResolver GetDbContextResolver() => _contextResolver;

private PageManager GetPageManager()
{
if (Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public class AuthorizedTodoItemsRepository : DefaultEntityRepository<TodoItem>
public AuthorizedTodoItemsRepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext,
IDbContextResolver contextResolver,
IAuthorizationService authService)
: base(loggerFactory, jsonApiContext)
: base(loggerFactory, jsonApiContext, contextResolver)
{
_logger = loggerFactory.CreateLogger<AuthorizedTodoItemsRepository>();
_authService = authService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
<PackageReference Include="Microsoft.DotNet.InternalAbstractions" Version="1.0.0" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>


<ItemGroup>
<DotNetCliToolReference Include="dotnet-xunit" Version="$(XUnitVersion)" />
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -36,7 +36,7 @@ public void AddJsonApiInternals_Adds_All_Required_Services()
var provider = services.BuildServiceProvider();

// assert
Assert.NotNull(provider.GetService<DbContext>());
Assert.NotNull(provider.GetService<IDbContextResolver>());
Assert.NotNull(provider.GetService(typeof(IEntityRepository<TodoItem>)));
Assert.NotNull(provider.GetService<JsonApiOptions>());
Assert.NotNull(provider.GetService<IContextGraph>());
Expand Down
7 changes: 2 additions & 5 deletions test/UnitTests/Data/DefaultEntityRepository_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,10 @@ private DefaultEntityRepository<TodoItem> GetRepository()
.Setup(m => m.RelationshipsToUpdate)
.Returns(_relationshipsToUpdate);

_jsonApiContextMock
.Setup(m => m.GetDbContextResolver())
.Returns(_contextResolverMock.Object);

return new DefaultEntityRepository<TodoItem>(
_loggFactoryMock.Object,
_jsonApiContextMock.Object);
_jsonApiContextMock.Object,
_contextResolverMock.Object);
}
}
}