Skip to content

Commit d555bb5

Browse files
author
Bart Koelman
authored
Refactorings (#1038)
Various refactorings * Renamed CollectionNotEmptyExpression to HasExpression * Renamed EqualsAnyOfExpression to AnyExpression * Changed SparseFieldSetExpression.Fields type from IReadOnlyCollection to IImmutableSet * Changed SparseFieldTableExpression.Table type from IReadOnlyDictionary to IImmutableDictionary and fixed equality comparison to discard order * Changed SortExpression.Elements type from IReadOnlyDictionary to IImmutableList * Changed ResourceFieldChainExpression.Fields type from IReadOnlyCollection to IImmutableList * Changed PaginationQueryStringValueExpression.Elements type from IReadOnlyCollection to IImmutableList * Changed AnyExpression.Constants type from IReadOnlyCollection to IImmutableSet * Changed LogicalExpression.Terms type from IReadOnlyCollection to IImmutableList * Changed IncludeExpression.Elements type and IncludeElementExpression.Children type from IReadOnlyCollection to IImmutableList * Add support for `IReadOnlySet<>` usage in resource models * Changed DisableQueryStringAttribute.ParameterNames type from IReadOnlyCollection to IReadOnlySet * Changed IResourceContextProvider.GetResourceContexts() to return a set instead of a list. Made ResourceContext sealed and immutable and implemented Equals/GetHashCode so it works with sets. * Use "endpoint" in RequestMethodNotAllowedException message * Renamed StandardQueryStringParameters to JsonApiQueryStringParameters * Replaced obsolete IQueryLayerComposer.GetResourceDefinitionAccessor() with injected service * Replaced obsolete IResourceFactory.GetResourceDefinitionAccessor() with injected service * Made TId in IResourceDefinition contravariant * Corrected terminology: relationships use left/right instead of primary/secondary * Removed obsolete IJsonApiRequest.BasePath * Removed EntityFrameworkCoreSupport (no longer needed for EF Core 5+) * Always set IJsonApiRequest.OperationKind from middleware * Renamed OperationKind to WriteOperationKind, exposed as IJsonApiRequest.WriteOperation * Add HTTP Method to trace logging
1 parent 3e982ec commit d555bb5

File tree

150 files changed

+1132
-1211
lines changed

Some content is hidden

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

150 files changed

+1132
-1211
lines changed

benchmarks/Serialization/JsonApiDeserializerBenchmarks.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ public JsonApiDeserializerBenchmarks()
3838
IResourceGraph resourceGraph = _dependencyFactory.CreateResourceGraph(options);
3939

4040
var serviceContainer = new ServiceContainer();
41-
serviceContainer.AddService(typeof(IResourceDefinitionAccessor), new ResourceDefinitionAccessor(resourceGraph, serviceContainer));
41+
var resourceDefinitionAccessor = new ResourceDefinitionAccessor(resourceGraph, serviceContainer);
42+
43+
serviceContainer.AddService(typeof(IResourceDefinitionAccessor), resourceDefinitionAccessor);
4244
serviceContainer.AddService(typeof(IResourceDefinition<BenchmarkResource>), new JsonApiResourceDefinition<BenchmarkResource>(resourceGraph));
4345

4446
var targetedFields = new TargetedFields();
4547
var request = new JsonApiRequest();
4648
var resourceFactory = new ResourceFactory(serviceContainer);
4749
var httpContextAccessor = new HttpContextAccessor();
4850

49-
_jsonApiDeserializer = new RequestDeserializer(resourceGraph, resourceFactory, targetedFields, httpContextAccessor, request, options);
51+
_jsonApiDeserializer = new RequestDeserializer(resourceGraph, resourceFactory, targetedFields, httpContextAccessor, request, options,
52+
resourceDefinitionAccessor);
5053
}
5154

5255
[Benchmark]

docs/internals/queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Processing a request involves the following steps:
1818
- The readers also implement `IQueryConstraintProvider`, which exposes expressions through `ExpressionInScope` objects.
1919
- `QueryLayerComposer` (used from `JsonApiResourceService`) collects all query constraints.
2020
- It combines them with default options and `IResourceDefinition` overrides and composes a tree of `QueryLayer` objects.
21-
- It lifts the tree for nested endpoints like /blogs/1/articles and rewrites includes.
21+
- It lifts the tree for secondary endpoints like /blogs/1/articles and rewrites includes.
2222
- `JsonApiResourceService` contains no more usage of `IQueryable`.
2323
- `EntityFrameworkCoreRepository` delegates to `QueryableBuilder` to transform the `QueryLayer` tree into `IQueryable` expression trees.
2424
`QueryBuilder` depends on `QueryClauseBuilder` implementations that visit the tree nodes, transforming them to `System.Linq.Expression` equivalents.

docs/usage/extensibility/resource-definitions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ public class EmployeeDefinition : JsonApiResourceDefinition<Employee>
202202
{
203203
}
204204

205-
public override IReadOnlyCollection<IncludeElementExpression> OnApplyIncludes(
206-
IReadOnlyCollection<IncludeElementExpression> existingIncludes)
205+
public override IImmutableList<IncludeElementExpression> OnApplyIncludes(
206+
IImmutableList<IncludeElementExpression> existingIncludes)
207207
{
208208
if (existingIncludes.Any(include =>
209209
include.Relationship.Property.Name == nameof(Employee.Manager)))

docs/usage/extensibility/services.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ public class TodoItemService : JsonApiResourceService<TodoItem>
1818
public TodoItemService(IResourceRepositoryAccessor repositoryAccessor,
1919
IQueryLayerComposer queryLayerComposer, IPaginationContext paginationContext,
2020
IJsonApiOptions options, ILoggerFactory loggerFactory, IJsonApiRequest request,
21-
IResourceChangeTracker<TodoItem> resourceChangeTracker)
21+
IResourceChangeTracker<TodoItem> resourceChangeTracker,
22+
IResourceDefinitionAccessor resourceDefinitionAccessor)
2223
: base(repositoryAccessor, queryLayerComposer, paginationContext, options,
23-
loggerFactory, request, resourceChangeTracker)
24+
loggerFactory, request, resourceChangeTracker, resourceDefinitionAccessor)
2425
{
2526
_notificationService = notificationService;
2627
}

docs/usage/reading/including-relationships.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ which is equivalent to:
6262
GET /api/articles?include=author&include=author.livingAddress&include=author.livingAddress.country
6363
```
6464

65-
This can be used on nested endpoints too:
65+
This can be used on secondary endpoints too:
6666

6767
```http
6868
GET /api/blogs/1/articles?include=author.livingAddress.country

docs/usage/reading/pagination.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ Resources can be paginated. This request would fetch the second page of 10 artic
66
GET /articles?page[size]=10&page[number]=2 HTTP/1.1
77
```
88

9-
## Nesting
10-
11-
Pagination can be used on nested endpoints, such as:
9+
Pagination can be used on secondary endpoints, such as:
1210

1311
```http
1412
GET /blogs/1/articles?page[number]=2 HTTP/1.1

docs/usage/reading/sorting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ GET /api/blogs?sort=count(articles) HTTP/1.1
3434

3535
This sorts the list of blogs by their number of articles.
3636

37-
## Nesting
37+
## Secondary endpoints
3838

39-
Sorting can be used on nested endpoints, such as:
39+
Sorting can be used on secondary endpoints, such as:
4040

4141
```http
4242
GET /api/blogs/1/articles?sort=caption HTTP/1.1

docs/usage/reading/sparse-fieldset-selection.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
As an alternative to returning all fields (attributes and relationships) from a resource, the `fields[]` query string parameter can be used to select a subset.
44
Put the resource type to apply the fieldset on between the brackets.
5-
This can be used on the resource being requested, as well as on nested endpoints and/or included resources.
5+
This can be used on primary and secondary endpoints. The selection is applied on both primary and included resources.
66

7-
Top-level example:
7+
Primary endpoint example:
88

99
```http
1010
GET /articles?fields[articles]=title,body,comments HTTP/1.1
1111
```
1212

13-
Nested endpoint example:
13+
Secondary endpoint example:
1414

1515
```http
1616
GET /api/blogs/1/articles?fields[articles]=title,body,comments HTTP/1.1

src/Examples/JsonApiDotNetCoreExample/Definitions/TodoItemDefinition.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ private SortExpression GetDefaultSortOrder()
3636
});
3737
}
3838

39-
public override Task OnWritingAsync(TodoItem resource, OperationKind operationKind, CancellationToken cancellationToken)
39+
public override Task OnWritingAsync(TodoItem resource, WriteOperationKind writeOperation, CancellationToken cancellationToken)
4040
{
41-
if (operationKind == OperationKind.CreateResource)
41+
if (writeOperation == WriteOperationKind.CreateResource)
4242
{
4343
resource.CreatedAt = _systemClock.UtcNow;
4444
}
45-
else if (operationKind == OperationKind.UpdateResource)
45+
else if (writeOperation == WriteOperationKind.UpdateResource)
4646
{
4747
resource.LastModifiedAt = _systemClock.UtcNow;
4848
}

src/Examples/MultiDbContextExample/Repositories/DbContextARepository.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public sealed class DbContextARepository<TResource> : EntityFrameworkCoreReposit
1414
where TResource : class, IIdentifiable<int>
1515
{
1616
public DbContextARepository(ITargetedFields targetedFields, DbContextResolver<DbContextA> contextResolver, IResourceGraph resourceGraph,
17-
IResourceFactory resourceFactory, IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory)
18-
: base(targetedFields, contextResolver, resourceGraph, resourceFactory, constraintProviders, loggerFactory)
17+
IResourceFactory resourceFactory, IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory,
18+
IResourceDefinitionAccessor resourceDefinitionAccessor)
19+
: base(targetedFields, contextResolver, resourceGraph, resourceFactory, constraintProviders, loggerFactory, resourceDefinitionAccessor)
1920
{
2021
}
2122
}

0 commit comments

Comments
 (0)