Skip to content

Commit 8cca15a

Browse files
authored
Merge pull request #529 from json-api-dotnet/feat/simplification-hooks
Improving testability and intuitivity of hooks
2 parents 9aea5de + 9ab7b35 commit 8cca15a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+541
-532
lines changed

JsonApiDotnetCore.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7A2B7ADD-ECB
1010
EndProject
1111
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}"
1212
EndProject
13-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreExampleTests", "test\JsonApiDotNetCoreExampleTests\JsonApiDotNetCoreExampleTests.csproj", "{0B959765-40D2-43B5-87EE-FE2FEF9DBED5}"
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCoreExampleTests", "test\JsonApiDotNetCoreExampleTests\JsonApiDotNetCoreExampleTests.csproj", "{0B959765-40D2-43B5-87EE-FE2FEF9DBED5}"
1414
EndProject
1515
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C5B4D998-CECB-454D-9F32-085A897577BE}"
1616
ProjectSection(SolutionItems) = preProject

benchmarks/Serialization/JsonApiDeserializer_Benchmarks.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ public JsonApiDeserializer_Benchmarks() {
4545
jsonApiOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
4646
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
4747

48-
var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();
4948

50-
_jsonApiDeSerializer = new JsonApiDeSerializer(jsonApiContextMock.Object, genericProcessorFactoryMock.Object);
49+
_jsonApiDeSerializer = new JsonApiDeSerializer(jsonApiContextMock.Object);
5150
}
5251

5352
[Benchmark]

src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override async Task<IActionResult> PatchAsync(Guid id, [FromBody] TodoIte
3333
if (entity.Name == "PRE-ATTACH-TEST")
3434
{
3535
var targetTodoId = entity.TodoItems.First().Id;
36-
var todoItemContext = _dbResolver.GetDbSet<TodoItem>();
36+
var todoItemContext = _dbResolver.GetContext().Set<TodoItem>();
3737
await todoItemContext.Where(ti => ti.Id == targetTodoId).FirstOrDefaultAsync();
3838
}
3939
return await base.PatchAsync(id, entity);

src/Examples/JsonApiDotNetCoreExample/Resources/PassportResource.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override void BeforeRead(ResourcePipeline pipeline, bool isIncluded = fal
2222
}
2323
}
2424

25-
public override void BeforeImplicitUpdateRelationship(IAffectedRelationships<Passport> resourcesByRelationship, ResourcePipeline pipeline)
25+
public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<Passport> resourcesByRelationship, ResourcePipeline pipeline)
2626
{
2727
resourcesByRelationship.GetByRelationship<Person>().ToList().ForEach(kvp => DoesNotTouchLockedPassports(kvp.Value));
2828
}

src/Examples/JsonApiDotNetCoreExample/Resources/PersonResource.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class PersonResource : LockableResourceBase<Person>
1010
{
1111
public PersonResource(IResourceGraph graph) : base(graph) { }
1212

13-
public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IAffectedRelationships<Person> resourcesByRelationship, ResourcePipeline pipeline)
13+
public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids, IRelationshipsDictionary<Person> resourcesByRelationship, ResourcePipeline pipeline)
1414
{
1515
BeforeImplicitUpdateRelationship(resourcesByRelationship, pipeline);
1616
return ids;
@@ -22,7 +22,7 @@ public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids
2222
// return entityDiff.Entities;
2323
//}
2424

25-
public override void BeforeImplicitUpdateRelationship(IAffectedRelationships<Person> resourcesByRelationship, ResourcePipeline pipeline)
25+
public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<Person> resourcesByRelationship, ResourcePipeline pipeline)
2626
{
2727
resourcesByRelationship.GetByRelationship<Passport>().ToList().ForEach(kvp => DisallowLocked(kvp.Value));
2828
}

src/Examples/JsonApiDotNetCoreExample/Resources/TagResource.cs

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public TagResource(IResourceGraph graph) : base(graph)
1313
{
1414
}
1515

16+
public override IEnumerable<Tag> BeforeCreate(IEntityHashSet<Tag> affected, ResourcePipeline pipeline)
17+
{
18+
return base.BeforeCreate(affected, pipeline);
19+
}
20+
1621
public override IEnumerable<Tag> OnReturn(HashSet<Tag> entities, ResourcePipeline pipeline)
1722
{
1823
return entities.Where(t => t.Name != "This should be not be included");

src/Examples/JsonApiDotNetCoreExample/Resources/TodoResource.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public override void BeforeRead(ResourcePipeline pipeline, bool isIncluded = fal
1919
}
2020
}
2121

22-
public override void BeforeImplicitUpdateRelationship(IAffectedRelationships<TodoItem> resourcesByRelationship, ResourcePipeline pipeline)
22+
public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<TodoItem> resourcesByRelationship, ResourcePipeline pipeline)
2323
{
2424
List<TodoItem> todos = resourcesByRelationship.GetByRelationship<Person>().SelectMany(kvp => kvp.Value).ToList();
2525
DisallowLocked(todos);

src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs

-6
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,6 @@ public class JsonApiOptions
181181
/// </example>
182182
public bool ValidateModelState { get; set; }
183183

184-
[Obsolete("JsonContract resolver can now be set on SerializerSettings.")]
185-
public IContractResolver JsonContractResolver
186-
{
187-
get => SerializerSettings.ContractResolver;
188-
set => SerializerSettings.ContractResolver = value;
189-
}
190184
public JsonSerializerSettings SerializerSettings { get; } = new JsonSerializerSettings()
191185
{
192186
NullValueHandling = NullValueHandling.Ignore,

src/JsonApiDotNetCore/Data/DbContextResolver.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public DbContextResolver(TContext context)
1515

1616
public DbContext GetContext() => _context;
1717

18-
public DbSet<TEntity> GetDbSet<TEntity>() where TEntity : class
19-
=> _context.GetDbSet<TEntity>();
18+
public DbSet<TEntity> GetDbSet<TEntity>() where TEntity : class => null;
19+
2020
}
2121
}

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public DefaultEntityRepository(
5656
ResourceDefinition<TEntity> resourceDefinition = null)
5757
{
5858
_context = contextResolver.GetContext();
59-
_dbSet = contextResolver.GetDbSet<TEntity>();
59+
_dbSet = _context.Set<TEntity>();
6060
_jsonApiContext = jsonApiContext;
6161
_genericProcessorFactory = _jsonApiContext.GenericProcessorFactory;
6262
_resourceDefinition = resourceDefinition;
@@ -69,7 +69,7 @@ public DefaultEntityRepository(
6969
ResourceDefinition<TEntity> resourceDefinition = null)
7070
{
7171
_context = contextResolver.GetContext();
72-
_dbSet = contextResolver.GetDbSet<TEntity>();
72+
_dbSet = _context.Set<TEntity>();
7373
_jsonApiContext = jsonApiContext;
7474
_logger = loggerFactory.CreateLogger<DefaultEntityRepository<TEntity, TId>>();
7575
_genericProcessorFactory = _jsonApiContext.GenericProcessorFactory;

src/JsonApiDotNetCore/Data/IDbContextResolver.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using System;
12
using Microsoft.EntityFrameworkCore;
23

34
namespace JsonApiDotNetCore.Data
45
{
56
public interface IDbContextResolver
67
{
78
DbContext GetContext();
9+
10+
[Obsolete("Use DbContext.Set<TEntity>() instead", error: true)]
811
DbSet<TEntity> GetDbSet<TEntity>()
912
where TEntity : class;
1013
}

src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs

-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ private static MethodInfo ContainsMethod
3030
}
3131
}
3232

33-
[Obsolete("Use overload Sort<T>(IJsonApiContext, List<SortQuery>) instead.", error: true)]
34-
public static IQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, List<SortQuery> sortQueries) => null;
35-
36-
[Obsolete("Use overload Sort<T>(IJsonApiContext, SortQuery) instead.", error: true)]
37-
public static IOrderedQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, SortQuery sortQuery) => null;
38-
39-
[Obsolete("Use overload Sort<T>(IJsonApiContext, SortQuery) instead.", error: true)]
40-
public static IOrderedQueryable<TSource> Sort<TSource>(this IOrderedQueryable<TSource> source, SortQuery sortQuery) => null;
41-
4233
public static IQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, IJsonApiContext jsonApiContext, List<SortQuery> sortQueries)
4334
{
4435
if (sortQueries == null || sortQueries.Count == 0)

src/JsonApiDotNetCore/Extensions/ModelStateExtensions.cs

-20
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,6 @@ namespace JsonApiDotNetCore.Extensions
77
{
88
public static class ModelStateExtensions
99
{
10-
[Obsolete("Use Generic Method ConvertToErrorCollection<T>(IResourceGraph resourceGraph) instead for full validation errors")]
11-
public static ErrorCollection ConvertToErrorCollection(this ModelStateDictionary modelState)
12-
{
13-
ErrorCollection collection = new ErrorCollection();
14-
foreach (var entry in modelState)
15-
{
16-
if (entry.Value.Errors.Any() == false)
17-
continue;
18-
19-
foreach (var modelError in entry.Value.Errors)
20-
{
21-
if (modelError.Exception is JsonApiException jex)
22-
collection.Errors.AddRange(jex.GetError().Errors);
23-
else
24-
collection.Errors.Add(new Error(400, entry.Key, modelError.ErrorMessage, modelError.Exception != null ? ErrorMeta.FromException(modelError.Exception) : null));
25-
}
26-
}
27-
28-
return collection;
29-
}
3010
public static ErrorCollection ConvertToErrorCollection<T>(this ModelStateDictionary modelState, IResourceGraph resourceGraph)
3111
{
3212
ErrorCollection collection = new ErrorCollection();

src/JsonApiDotNetCore/Hooks/Execution/AffectedRelationships.cs

-54
This file was deleted.

src/JsonApiDotNetCore/Hooks/Execution/AffectedResourceDiff.cs

-69
This file was deleted.

src/JsonApiDotNetCore/Hooks/Execution/AffectedResources.cs

-37
This file was deleted.

0 commit comments

Comments
 (0)