Class JsonApiResourceDefinition<TResource, TId>
Provides an extensibility point to add business logic that is resource-oriented instead of endpoint-oriented.
Inheritance
Implements
Inherited Members
Namespace: JsonApiDotNetCore.Resources
Assembly: JsonApiDotNetCore.dll
Syntax
[PublicAPI]
public class JsonApiResourceDefinition<TResource, TId> : IResourceDefinition<TResource, TId> where TResource : class, IIdentifiable<TId>
Type Parameters
Name | Description |
---|---|
TResource | The resource type. |
TId | The resource identifier type. |
Constructors
| Improve this Doc View SourceJsonApiResourceDefinition(IResourceGraph)
Declaration
public JsonApiResourceDefinition(IResourceGraph resourceGraph)
Parameters
Type | Name | Description |
---|---|---|
IResourceGraph | resourceGraph |
Properties
| Improve this Doc View SourceResourceGraph
Declaration
protected IResourceGraph ResourceGraph { get; }
Property Value
Type | Description |
---|---|
IResourceGraph |
ResourceType
Provides metadata for the resource type TResource
.
Declaration
protected ResourceType ResourceType { get; }
Property Value
Type | Description |
---|---|
ResourceType |
Methods
| Improve this Doc View SourceCreateSortExpressionFromLambda(JsonApiResourceDefinition<TResource, TId>.PropertySortOrder)
Creates a SortExpression from a lambda expression.
Declaration
protected SortExpression CreateSortExpressionFromLambda(JsonApiResourceDefinition<TResource, TId>.PropertySortOrder keySelectors)
Parameters
Type | Name | Description |
---|---|---|
JsonApiResourceDefinition.PropertySortOrder<> | keySelectors |
Returns
Type | Description |
---|---|
SortExpression |
Examples
var sort = CreateSortExpressionFromLambda(new PropertySortOrder
{
(blog => blog.Author.Name.LastName, ListSortDirection.Ascending),
(blog => blog.Posts.Count, ListSortDirection.Descending),
(blog => blog.Title, ListSortDirection.Ascending)
});
|
Improve this Doc
View Source
GetMeta(TResource)
Enables to add JSON:API meta information, specific to this resource.
Declaration
public virtual IDictionary<string, object>? GetMeta(TResource resource)
Parameters
Type | Name | Description |
---|---|---|
TResource | resource |
Returns
Type | Description |
---|---|
System.Nullable<IDictionary<System.String, System.Object>> |
OnAddToRelationshipAsync(TResource, HasManyAttribute, ISet<IIdentifiable>, CancellationToken)
Executes before adding resources to the right side of a to-many relationship, as part of a POST relationship request.
Implementing this method enables to perform validations and make changes to rightResourceIds
, before the relationship is updated.
Declaration
public virtual Task OnAddToRelationshipAsync(TResource leftResource, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
TResource | leftResource | Identifier of the left resource. The indication "left" specifies that
|
HasManyAttribute | hasManyRelationship | The to-many relationship being added to. |
ISet<IIdentifiable> | rightResourceIds | The set of resource identifiers to add to the to-many relationship, coming from the request. |
CancellationToken | cancellationToken | Propagates notification that request handling should be canceled. |
Returns
Type | Description |
---|---|
Task |
OnApplyFilter(FilterExpression)
Enables to extend, replace or remove a filter that is being applied on a set of this resource type.
Declaration
public virtual FilterExpression OnApplyFilter(FilterExpression existingFilter)
Parameters
Type | Name | Description |
---|---|---|
FilterExpression | existingFilter | An optional existing filter, coming from query string. Can be |
Returns
Type | Description |
---|---|
FilterExpression | The new filter, or |
OnApplyIncludes(IImmutableSet<IncludeElementExpression>)
Enables to extend, replace or remove includes that are being applied on this resource type.
Declaration
public virtual IImmutableSet<IncludeElementExpression> OnApplyIncludes(IImmutableSet<IncludeElementExpression> existingIncludes)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Immutable.IImmutableSet<IncludeElementExpression> | existingIncludes | An optional existing set of includes, coming from query string. Never |
Returns
Type | Description |
---|---|
System.Collections.Immutable.IImmutableSet<IncludeElementExpression> | The new set of includes. Return an empty collection to remove all inclusions (never return |
OnApplyPagination(PaginationExpression)
Enables to extend, replace or remove pagination that is being applied on a set of this resource type.
Declaration
public virtual PaginationExpression OnApplyPagination(PaginationExpression existingPagination)
Parameters
Type | Name | Description |
---|---|---|
PaginationExpression | existingPagination | An optional existing pagination, coming from query string. Can be |
Returns
Type | Description |
---|---|
PaginationExpression | The changed pagination, or |
OnApplySort(SortExpression)
Enables to extend, replace or remove a sort order that is being applied on a set of this resource type. Tip: Use CreateSortExpressionFromLambda(JsonApiResourceDefinition<TResource, TId>.PropertySortOrder) to build from a lambda expression.
Declaration
public virtual SortExpression OnApplySort(SortExpression existingSort)
Parameters
Type | Name | Description |
---|---|---|
SortExpression | existingSort | An optional existing sort order, coming from query string. Can be |
Returns
Type | Description |
---|---|
SortExpression | The new sort order, or |
OnApplySparseFieldSet(SparseFieldSetExpression)
Enables to extend, replace or remove a sparse fieldset that is being applied on a set of this resource type. Tip: Use Including<TResource>(SparseFieldSetExpression, Expression<Func<TResource, Object>>, IResourceGraph) and Excluding<TResource>(SparseFieldSetExpression, Expression<Func<TResource, Object>>, IResourceGraph) to safely change the fieldset without worrying about nulls.
Declaration
public virtual SparseFieldSetExpression OnApplySparseFieldSet(SparseFieldSetExpression existingSparseFieldSet)
Parameters
Type | Name | Description |
---|---|---|
SparseFieldSetExpression | existingSparseFieldSet | The incoming sparse fieldset from query string. At query execution time, this is |
Returns
Type | Description |
---|---|
SparseFieldSetExpression | The new sparse fieldset, or |
Remarks
This method executes twice for a single request: first to select which fields to retrieve from the data store and then to select which fields to serialize. Including extra fields from this method will retrieve them, but not include them in the json output. This enables you to expose calculated properties whose value depends on a field that is not in the sparse fieldset.
OnDeserialize(TResource)
Executes after a resource has been deserialized from an incoming request body.
Declaration
public virtual void OnDeserialize(TResource resource)
Parameters
Type | Name | Description |
---|---|---|
TResource | resource | The deserialized resource. |
OnPrepareWriteAsync(TResource, WriteOperationKind, CancellationToken)
Executes after the original version of the resource has been retrieved from the underlying data store, as part of a write request.
Implementing this method enables to perform validations and make changes to resource
, before the fields from the request are
copied into it.
For POST resource requests, this method is typically used to assign property default values or to set required relationships by side-loading the related resources and linking them.
Declaration
public virtual Task OnPrepareWriteAsync(TResource resource, WriteOperationKind writeOperation, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
TResource | resource | The original resource retrieved from the underlying data store, or a freshly instantiated resource in case of a POST resource request. |
WriteOperationKind | writeOperation | Identifies the logical write operation for which this method was called. Possible values: CreateResource, UpdateResource, SetRelationship and RemoveFromRelationship. Note this intentionally excludes DeleteResource and AddToRelationship, because for those endpoints no resource is retrieved upfront. |
CancellationToken | cancellationToken | Propagates notification that request handling should be canceled. |
Returns
Type | Description |
---|---|
Task |
OnRegisterQueryableHandlersForQueryStringParameters()
Enables to adapt the Entity Framework Core
Declaration
public virtual QueryStringParameterHandlers<TResource> OnRegisterQueryableHandlersForQueryStringParameters()
Returns
Type | Description |
---|---|
QueryStringParameterHandlers<TResource> |
OnRemoveFromRelationshipAsync(TResource, HasManyAttribute, ISet<IIdentifiable>, CancellationToken)
Executes before removing resources from the right side of a to-many relationship, as part of a DELETE relationship request.
Implementing this method enables to perform validations and make changes to rightResourceIds
, before the relationship is updated.
Declaration
public virtual Task OnRemoveFromRelationshipAsync(TResource leftResource, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
TResource | leftResource | The original resource as retrieved from the underlying data store. The indication "left" specifies that |
HasManyAttribute | hasManyRelationship | The to-many relationship being removed from. |
ISet<IIdentifiable> | rightResourceIds | The set of resource identifiers to remove from the to-many relationship, coming from the request. |
CancellationToken | cancellationToken | Propagates notification that request handling should be canceled. |
Returns
Type | Description |
---|---|
Task |
OnSerialize(TResource)
Executes before a (primary or included) resource is serialized into an outgoing response body.
Declaration
public virtual void OnSerialize(TResource resource)
Parameters
Type | Name | Description |
---|---|---|
TResource | resource | The serialized resource. |
OnSetToManyRelationshipAsync(TResource, HasManyAttribute, ISet<IIdentifiable>, WriteOperationKind, CancellationToken)
Executes before setting the resources at the right side of a to-many relationship. This replaces on existing set.
Implementing this method enables to perform validations and make changes to rightResourceIds
, before the relationship is updated.
Declaration
public virtual Task OnSetToManyRelationshipAsync(TResource leftResource, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds, WriteOperationKind writeOperation, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
TResource | leftResource | The original resource as retrieved from the underlying data store. The indication "left" specifies that |
HasManyAttribute | hasManyRelationship | The to-many relationship being set. |
ISet<IIdentifiable> | rightResourceIds | The set of resource identifiers to replace any existing set with, coming from the request. |
WriteOperationKind | writeOperation | Identifies the logical write operation for which this method was called. Possible values: CreateResource, UpdateResource and SetRelationship. |
CancellationToken | cancellationToken | Propagates notification that request handling should be canceled. |
Returns
Type | Description |
---|---|
Task |
OnSetToOneRelationshipAsync(TResource, HasOneAttribute, IIdentifiable, WriteOperationKind, CancellationToken)
Executes before setting (or clearing) the resource at the right side of a to-one relationship.
Implementing this method enables to perform validations and change rightResourceId
, before the relationship is updated.
Declaration
public virtual Task<IIdentifiable> OnSetToOneRelationshipAsync(TResource leftResource, HasOneAttribute hasOneRelationship, IIdentifiable rightResourceId, WriteOperationKind writeOperation, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
TResource | leftResource | The original resource as retrieved from the underlying data store. The indication "left" specifies that |
HasOneAttribute | hasOneRelationship | The to-one relationship being set. |
IIdentifiable | rightResourceId | The new resource identifier (or |
WriteOperationKind | writeOperation | Identifies the logical write operation for which this method was called. Possible values: CreateResource, UpdateResource and SetRelationship. |
CancellationToken | cancellationToken | Propagates notification that request handling should be canceled. |
Returns
Type | Description |
---|---|
Task<IIdentifiable> | The replacement resource identifier, or |
OnWriteSucceededAsync(TResource, WriteOperationKind, CancellationToken)
Executes after successfully writing the changed resource to the underlying data store, as part of a write request.
Implementing this method enables to run additional logic, for example enqueue a notification message on a service bus.
Declaration
public virtual Task OnWriteSucceededAsync(TResource resource, WriteOperationKind writeOperation, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
TResource | resource | The resource as written to the underlying data store. |
WriteOperationKind | writeOperation | Identifies the logical write operation for which this method was called. Possible values: CreateResource, UpdateResource, DeleteResource, SetRelationship , AddToRelationship and RemoveFromRelationship. |
CancellationToken | cancellationToken | Propagates notification that request handling should be canceled. |
Returns
Type | Description |
---|---|
Task |
OnWritingAsync(TResource, WriteOperationKind, CancellationToken)
Executes before writing the changed resource to the underlying data store, as part of a write request.
Implementing this method enables to perform validations and make changes to resource
, after the fields from the request have been
copied into it.
An example usage is to set the last-modification timestamp, overwriting the value from the incoming request.
Another use case is to add a notification message to an outbox table, which gets committed along with the resource write in a single transaction (see https://microservices.io/patterns/data/transactional-outbox.html).
Declaration
public virtual Task OnWritingAsync(TResource resource, WriteOperationKind writeOperation, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
TResource | resource | The original resource retrieved from the underlying data store (or a freshly instantiated resource in case of a POST resource request), updated with
the changes from the incoming request. Exception: In case |
WriteOperationKind | writeOperation | Identifies the logical write operation for which this method was called. Possible values: CreateResource, UpdateResource, DeleteResource, SetRelationship , AddToRelationship and RemoveFromRelationship. |
CancellationToken | cancellationToken | Propagates notification that request handling should be canceled. |
Returns
Type | Description |
---|---|
Task |