Table of Contents

Class JsonApiResourceService<TResource, TId>

Namespace
JsonApiDotNetCore.Services
Assembly
JsonApiDotNetCore.dll

Represents the foundational Resource Service layer in the JsonApiDotNetCore architecture that uses a Resource Repository for data access.

[PublicAPI]
public class JsonApiResourceService<TResource, TId> : IResourceService<TResource, TId>, IResourceCommandService<TResource, TId>, ICreateService<TResource, TId>, IAddToRelationshipService<TResource, TId>, IUpdateService<TResource, TId>, ISetRelationshipService<TResource, TId>, IDeleteService<TResource, TId>, IRemoveFromRelationshipService<TResource, TId>, IResourceQueryService<TResource, TId>, IGetAllService<TResource, TId>, IGetByIdService<TResource, TId>, IGetRelationshipService<TResource, TId>, IGetSecondaryService<TResource, TId> where TResource : class, IIdentifiable<TId>

Type Parameters

TResource

The resource type.

TId

The resource identifier type.

Inheritance
JsonApiResourceService<TResource, TId>
Implements
IResourceService<TResource, TId>
IResourceCommandService<TResource, TId>
ICreateService<TResource, TId>
IUpdateService<TResource, TId>
ISetRelationshipService<TResource, TId>
IDeleteService<TResource, TId>
IResourceQueryService<TResource, TId>
IGetAllService<TResource, TId>
IGetByIdService<TResource, TId>
IGetRelationshipService<TResource, TId>
IGetSecondaryService<TResource, TId>
Inherited Members

Constructors

JsonApiResourceService(IResourceRepositoryAccessor, IQueryLayerComposer, IPaginationContext, IJsonApiOptions, ILoggerFactory, IJsonApiRequest, IResourceChangeTracker<TResource>, IResourceDefinitionAccessor)

public JsonApiResourceService(IResourceRepositoryAccessor repositoryAccessor, IQueryLayerComposer queryLayerComposer, IPaginationContext paginationContext, IJsonApiOptions options, ILoggerFactory loggerFactory, IJsonApiRequest request, IResourceChangeTracker<TResource> resourceChangeTracker, IResourceDefinitionAccessor resourceDefinitionAccessor)

Parameters

repositoryAccessor IResourceRepositoryAccessor
queryLayerComposer IQueryLayerComposer
paginationContext IPaginationContext
options IJsonApiOptions
loggerFactory ILoggerFactory
request IJsonApiRequest
resourceChangeTracker IResourceChangeTracker<TResource>
resourceDefinitionAccessor IResourceDefinitionAccessor

Methods

AddToToManyRelationshipAsync(TId, string, ISet<IIdentifiable>, CancellationToken)

Handles a JSON:API request to add resources to a to-many relationship.

public virtual Task AddToToManyRelationshipAsync(TId leftId, string relationshipName, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)

Parameters

leftId TId

Identifies the left side of the relationship.

relationshipName string

The relationship to add resources to.

rightResourceIds ISet<IIdentifiable>

The set of resources to add to the relationship.

cancellationToken CancellationToken

Propagates notification that request handling should be canceled.

Returns

Task

AssertPrimaryResourceDoesNotExistAsync(TResource, CancellationToken)

protected Task AssertPrimaryResourceDoesNotExistAsync(TResource resource, CancellationToken cancellationToken)

Parameters

resource TResource
cancellationToken CancellationToken

Returns

Task

AssertResourcesToAssignInRelationshipsExistAsync(TResource, CancellationToken)

protected Task AssertResourcesToAssignInRelationshipsExistAsync(TResource primaryResource, CancellationToken cancellationToken)

Parameters

primaryResource TResource
cancellationToken CancellationToken

Returns

Task

AssertRightResourcesExistAsync(object?, CancellationToken)

protected Task<object?> AssertRightResourcesExistAsync(object? rightValue, CancellationToken cancellationToken)

Parameters

rightValue object
cancellationToken CancellationToken

Returns

Task<object>

CreateAsync(TResource, CancellationToken)

Handles a JSON:API request to create a new resource with attributes, relationships or both.

public virtual Task<TResource?> CreateAsync(TResource resource, CancellationToken cancellationToken)

Parameters

resource TResource
cancellationToken CancellationToken

Returns

Task<TResource>

DeleteAsync(TId, CancellationToken)

Handles a JSON:API request to delete an existing resource.

public virtual Task DeleteAsync(TId id, CancellationToken cancellationToken)

Parameters

id TId
cancellationToken CancellationToken

Returns

Task

GetAsync(CancellationToken)

Handles a JSON:API request to retrieve a collection of resources for a primary endpoint.

public virtual Task<IReadOnlyCollection<TResource>> GetAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Returns

Task<IReadOnlyCollection<TResource>>

GetAsync(TId, CancellationToken)

Handles a JSON:API request to retrieve a single resource for a primary endpoint.

public virtual Task<TResource> GetAsync(TId id, CancellationToken cancellationToken)

Parameters

id TId
cancellationToken CancellationToken

Returns

Task<TResource>

GetPrimaryResourceByIdAsync(TId, TopFieldSelection, CancellationToken)

protected Task<TResource> GetPrimaryResourceByIdAsync(TId id, TopFieldSelection fieldSelection, CancellationToken cancellationToken)

Parameters

id TId
fieldSelection TopFieldSelection
cancellationToken CancellationToken

Returns

Task<TResource>

GetPrimaryResourceForUpdateAsync(TId, CancellationToken)

protected Task<TResource> GetPrimaryResourceForUpdateAsync(TId id, CancellationToken cancellationToken)

Parameters

id TId
cancellationToken CancellationToken

Returns

Task<TResource>

GetRelationshipAsync(TId, string, CancellationToken)

Handles a JSON:API request to retrieve a single relationship.

public virtual Task<object?> GetRelationshipAsync(TId id, string relationshipName, CancellationToken cancellationToken)

Parameters

id TId
relationshipName string
cancellationToken CancellationToken

Returns

Task<object>

GetSecondaryAsync(TId, string, CancellationToken)

Handles a JSON:API request to retrieve a single resource or a collection of resources for a secondary endpoint, such as /articles/1/author or /articles/1/revisions.

public virtual Task<object?> GetSecondaryAsync(TId id, string relationshipName, CancellationToken cancellationToken)

Parameters

id TId
relationshipName string
cancellationToken CancellationToken

Returns

Task<object>

InitializeResourceAsync(TResource, CancellationToken)

protected virtual Task InitializeResourceAsync(TResource resourceForDatabase, CancellationToken cancellationToken)

Parameters

resourceForDatabase TResource
cancellationToken CancellationToken

Returns

Task

RemoveFromToManyRelationshipAsync(TId, string, ISet<IIdentifiable>, CancellationToken)

Handles a JSON:API request to remove resources from a to-many relationship.

public virtual Task RemoveFromToManyRelationshipAsync(TId leftId, string relationshipName, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)

Parameters

leftId TId

Identifies the left side of the relationship.

relationshipName string

The relationship to remove resources from.

rightResourceIds ISet<IIdentifiable>

The set of resources to remove from the relationship.

cancellationToken CancellationToken

Propagates notification that request handling should be canceled.

Returns

Task

SetRelationshipAsync(TId, string, object?, CancellationToken)

Handles a JSON:API request to perform a complete replacement of a relationship on an existing resource.

public virtual Task SetRelationshipAsync(TId leftId, string relationshipName, object? rightValue, CancellationToken cancellationToken)

Parameters

leftId TId

Identifies the left side of the relationship.

relationshipName string

The relationship for which to perform a complete replacement.

rightValue object

The resource or set of resources to assign to the relationship.

cancellationToken CancellationToken

Propagates notification that request handling should be canceled.

Returns

Task

UpdateAsync(TId, TResource, CancellationToken)

Handles a JSON:API request to update the attributes and/or relationships of an existing resource. Only the values of sent attributes are replaced. And only the values of sent relationships are replaced.

public virtual Task<TResource?> UpdateAsync(TId id, TResource resource, CancellationToken cancellationToken)

Parameters

id TId
resource TResource
cancellationToken CancellationToken

Returns

Task<TResource>