Skip to content

Improving testability and intuitivity of hooks #529

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 12 commits into from
Jun 21, 2019
Merged
2 changes: 1 addition & 1 deletion JsonApiDotnetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7A2B7ADD-ECB
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreExampleTests", "test\JsonApiDotNetCoreExampleTests\JsonApiDotNetCoreExampleTests.csproj", "{0B959765-40D2-43B5-87EE-FE2FEF9DBED5}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCoreExampleTests", "test\JsonApiDotNetCoreExampleTests\JsonApiDotNetCoreExampleTests.csproj", "{0B959765-40D2-43B5-87EE-FE2FEF9DBED5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C5B4D998-CECB-454D-9F32-085A897577BE}"
ProjectSection(SolutionItems) = preProject
Expand Down
3 changes: 1 addition & 2 deletions benchmarks/Serialization/JsonApiDeserializer_Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public JsonApiDeserializer_Benchmarks() {
jsonApiOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);

var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();

_jsonApiDeSerializer = new JsonApiDeSerializer(jsonApiContextMock.Object, genericProcessorFactoryMock.Object);
_jsonApiDeSerializer = new JsonApiDeSerializer(jsonApiContextMock.Object);
}

[Benchmark]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override async Task<IActionResult> PatchAsync(Guid id, [FromBody] TodoIte
if (entity.Name == "PRE-ATTACH-TEST")
{
var targetTodoId = entity.TodoItems.First().Id;
var todoItemContext = _dbResolver.GetDbSet<TodoItem>();
var todoItemContext = _dbResolver.GetContext().Set<TodoItem>();
await todoItemContext.Where(ti => ti.Id == targetTodoId).FirstOrDefaultAsync();
}
return await base.PatchAsync(id, entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void BeforeRead(ResourcePipeline pipeline, bool isIncluded = fal
}
}

public override void BeforeImplicitUpdateRelationship(IAffectedRelationships<Passport> resourcesByRelationship, ResourcePipeline pipeline)
public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<Passport> resourcesByRelationship, ResourcePipeline pipeline)
{
resourcesByRelationship.GetByRelationship<Person>().ToList().ForEach(kvp => DoesNotTouchLockedPassports(kvp.Value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class PersonResource : LockableResourceBase<Person>
{
public PersonResource(IResourceGraph graph) : base(graph) { }

public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IAffectedRelationships<Person> resourcesByRelationship, ResourcePipeline pipeline)
public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IRelationshipsDictionary<Person> resourcesByRelationship, ResourcePipeline pipeline)
{
BeforeImplicitUpdateRelationship(resourcesByRelationship, pipeline);
return ids;
Expand All @@ -22,7 +22,7 @@ public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids
// return entityDiff.Entities;
//}

public override void BeforeImplicitUpdateRelationship(IAffectedRelationships<Person> resourcesByRelationship, ResourcePipeline pipeline)
public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<Person> resourcesByRelationship, ResourcePipeline pipeline)
{
resourcesByRelationship.GetByRelationship<Passport>().ToList().ForEach(kvp => DisallowLocked(kvp.Value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public TagResource(IResourceGraph graph) : base(graph)
{
}

public override IEnumerable<Tag> BeforeCreate(IEntityHashSet<Tag> affected, ResourcePipeline pipeline)
{
return base.BeforeCreate(affected, pipeline);
}

public override IEnumerable<Tag> OnReturn(HashSet<Tag> entities, ResourcePipeline pipeline)
{
return entities.Where(t => t.Name != "This should be not be included");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override void BeforeRead(ResourcePipeline pipeline, bool isIncluded = fal
}
}

public override void BeforeImplicitUpdateRelationship(IAffectedRelationships<TodoItem> resourcesByRelationship, ResourcePipeline pipeline)
public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<TodoItem> resourcesByRelationship, ResourcePipeline pipeline)
{
List<TodoItem> todos = resourcesByRelationship.GetByRelationship<Person>().SelectMany(kvp => kvp.Value).ToList();
DisallowLocked(todos);
Expand Down
6 changes: 0 additions & 6 deletions src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,6 @@ public class JsonApiOptions
/// </example>
public bool ValidateModelState { get; set; }

[Obsolete("JsonContract resolver can now be set on SerializerSettings.")]
public IContractResolver JsonContractResolver
{
get => SerializerSettings.ContractResolver;
set => SerializerSettings.ContractResolver = value;
}
public JsonSerializerSettings SerializerSettings { get; } = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Data/DbContextResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public DbContextResolver(TContext context)

public DbContext GetContext() => _context;

public DbSet<TEntity> GetDbSet<TEntity>() where TEntity : class
=> _context.GetDbSet<TEntity>();
public DbSet<TEntity> GetDbSet<TEntity>() where TEntity : class => null;

}
}
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public DefaultEntityRepository(
ResourceDefinition<TEntity> resourceDefinition = null)
{
_context = contextResolver.GetContext();
_dbSet = contextResolver.GetDbSet<TEntity>();
_dbSet = _context.Set<TEntity>();
_jsonApiContext = jsonApiContext;
_genericProcessorFactory = _jsonApiContext.GenericProcessorFactory;
_resourceDefinition = resourceDefinition;
Expand All @@ -69,7 +69,7 @@ public DefaultEntityRepository(
ResourceDefinition<TEntity> resourceDefinition = null)
{
_context = contextResolver.GetContext();
_dbSet = contextResolver.GetDbSet<TEntity>();
_dbSet = _context.Set<TEntity>();
_jsonApiContext = jsonApiContext;
_logger = loggerFactory.CreateLogger<DefaultEntityRepository<TEntity, TId>>();
_genericProcessorFactory = _jsonApiContext.GenericProcessorFactory;
Expand Down
3 changes: 3 additions & 0 deletions src/JsonApiDotNetCore/Data/IDbContextResolver.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using Microsoft.EntityFrameworkCore;

namespace JsonApiDotNetCore.Data
{
public interface IDbContextResolver
{
DbContext GetContext();

[Obsolete("Use DbContext.Set<TEntity>() instead", error: true)]
DbSet<TEntity> GetDbSet<TEntity>()
where TEntity : class;
}
Expand Down
9 changes: 0 additions & 9 deletions src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ private static MethodInfo ContainsMethod
}
}

[Obsolete("Use overload Sort<T>(IJsonApiContext, List<SortQuery>) instead.", error: true)]
public static IQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, List<SortQuery> sortQueries) => null;

[Obsolete("Use overload Sort<T>(IJsonApiContext, SortQuery) instead.", error: true)]
public static IOrderedQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, SortQuery sortQuery) => null;

[Obsolete("Use overload Sort<T>(IJsonApiContext, SortQuery) instead.", error: true)]
public static IOrderedQueryable<TSource> Sort<TSource>(this IOrderedQueryable<TSource> source, SortQuery sortQuery) => null;

public static IQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, IJsonApiContext jsonApiContext, List<SortQuery> sortQueries)
{
if (sortQueries == null || sortQueries.Count == 0)
Expand Down
20 changes: 0 additions & 20 deletions src/JsonApiDotNetCore/Extensions/ModelStateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@ namespace JsonApiDotNetCore.Extensions
{
public static class ModelStateExtensions
{
[Obsolete("Use Generic Method ConvertToErrorCollection<T>(IResourceGraph resourceGraph) instead for full validation errors")]
public static ErrorCollection ConvertToErrorCollection(this ModelStateDictionary modelState)
{
ErrorCollection collection = new ErrorCollection();
foreach (var entry in modelState)
{
if (entry.Value.Errors.Any() == false)
continue;

foreach (var modelError in entry.Value.Errors)
{
if (modelError.Exception is JsonApiException jex)
collection.Errors.AddRange(jex.GetError().Errors);
else
collection.Errors.Add(new Error(400, entry.Key, modelError.ErrorMessage, modelError.Exception != null ? ErrorMeta.FromException(modelError.Exception) : null));
}
}

return collection;
}
public static ErrorCollection ConvertToErrorCollection<T>(this ModelStateDictionary modelState, IResourceGraph resourceGraph)
{
ErrorCollection collection = new ErrorCollection();
Expand Down
54 changes: 0 additions & 54 deletions src/JsonApiDotNetCore/Hooks/Execution/AffectedRelationships.cs

This file was deleted.

69 changes: 0 additions & 69 deletions src/JsonApiDotNetCore/Hooks/Execution/AffectedResourceDiff.cs

This file was deleted.

37 changes: 0 additions & 37 deletions src/JsonApiDotNetCore/Hooks/Execution/AffectedResources.cs

This file was deleted.

Loading