Table of Contents

Class EntityFrameworkCoreRepository<TResource, TId>

Namespace
JsonApiDotNetCore.Repositories
Assembly
JsonApiDotNetCore.dll

Implements the foundational Repository layer in the JsonApiDotNetCore architecture that uses Entity Framework Core.

[PublicAPI]
public class EntityFrameworkCoreRepository<TResource, TId> : IResourceRepository<TResource, TId>, IResourceReadRepository<TResource, TId>, IResourceWriteRepository<TResource, TId>, IRepositorySupportsTransaction where TResource : class, IIdentifiable<TId>

Type Parameters

TResource

The resource type.

TId

The resource identifier type.

Inheritance
EntityFrameworkCoreRepository<TResource, TId>
Implements
IResourceRepository<TResource, TId>
IResourceReadRepository<TResource, TId>
IResourceWriteRepository<TResource, TId>
Inherited Members

Constructors

EntityFrameworkCoreRepository(ITargetedFields, IDbContextResolver, IResourceGraph, IResourceFactory, IEnumerable<IQueryConstraintProvider>, ILoggerFactory, IResourceDefinitionAccessor)

public EntityFrameworkCoreRepository(ITargetedFields targetedFields, IDbContextResolver dbContextResolver, IResourceGraph resourceGraph, IResourceFactory resourceFactory, IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory, IResourceDefinitionAccessor resourceDefinitionAccessor)

Parameters

targetedFields ITargetedFields
dbContextResolver IDbContextResolver
resourceGraph IResourceGraph
resourceFactory IResourceFactory
constraintProviders IEnumerable<IQueryConstraintProvider>
loggerFactory ILoggerFactory
resourceDefinitionAccessor IResourceDefinitionAccessor

Properties

TransactionId

Identifies the currently active transaction.

public virtual string? TransactionId { get; }

Property Value

string

Methods

AddToToManyRelationshipAsync(TResource?, TId, ISet<IIdentifiable>, CancellationToken)

Adds resources to a to-many relationship in the underlying data store.

public virtual Task AddToToManyRelationshipAsync(TResource? leftResource, TId leftId, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)

Parameters

leftResource TResource
leftId TId
rightResourceIds ISet<IIdentifiable>
cancellationToken CancellationToken

Returns

Task

ApplyQueryLayer(QueryLayer)

protected virtual IQueryable<TResource> ApplyQueryLayer(QueryLayer queryLayer)

Parameters

queryLayer QueryLayer

Returns

IQueryable<TResource>

AssertIsNotClearingRequiredToOneRelationship(RelationshipAttribute, object?)

protected void AssertIsNotClearingRequiredToOneRelationship(RelationshipAttribute relationship, object? rightValue)

Parameters

relationship RelationshipAttribute
rightValue object

CountAsync(FilterExpression?, CancellationToken)

Executes a read query using the specified filter and returns the count of matching resources.

public virtual Task<int> CountAsync(FilterExpression? filter, CancellationToken cancellationToken)

Parameters

filter FilterExpression
cancellationToken CancellationToken

Returns

Task<int>

CreateAsync(TResource, TResource, CancellationToken)

Creates a new resource in the underlying data store.

public virtual Task CreateAsync(TResource resourceFromRequest, TResource resourceForDatabase, CancellationToken cancellationToken)

Parameters

resourceFromRequest TResource
resourceForDatabase TResource
cancellationToken CancellationToken

Returns

Task

DeleteAsync(TResource?, TId, CancellationToken)

Deletes an existing resource from the underlying data store.

public virtual Task DeleteAsync(TResource? resourceFromDatabase, TId id, CancellationToken cancellationToken)

Parameters

resourceFromDatabase TResource
id TId
cancellationToken CancellationToken

Returns

Task

GetAll()

protected virtual IQueryable<TResource> GetAll()

Returns

IQueryable<TResource>

GetAsync(QueryLayer, CancellationToken)

Executes a read query using the specified constraints and returns the collection of matching resources.

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

Parameters

queryLayer QueryLayer
cancellationToken CancellationToken

Returns

Task<IReadOnlyCollection<TResource>>

GetForCreateAsync(Type, TId, CancellationToken)

Creates a new resource instance, in preparation for CreateAsync(TResource, TResource, CancellationToken).

public virtual Task<TResource> GetForCreateAsync(Type resourceClrType, TId id, CancellationToken cancellationToken)

Parameters

resourceClrType Type
id TId
cancellationToken CancellationToken

Returns

Task<TResource>

Remarks

This method can be overridden to assign resource-specific required relationships.

GetForUpdateAsync(QueryLayer, CancellationToken)

Retrieves a resource with all of its attributes, including the set of targeted relationships, in preparation for UpdateAsync(TResource, TResource, CancellationToken).

public virtual Task<TResource?> GetForUpdateAsync(QueryLayer queryLayer, CancellationToken cancellationToken)

Parameters

queryLayer QueryLayer
cancellationToken CancellationToken

Returns

Task<TResource>

GetTrackingBehavior()

protected virtual QueryTrackingBehavior? GetTrackingBehavior()

Returns

QueryTrackingBehavior?

RemoveFromToManyRelationshipAsync(TResource, ISet<IIdentifiable>, CancellationToken)

Removes resources from a to-many relationship in the underlying data store.

public virtual Task RemoveFromToManyRelationshipAsync(TResource leftResource, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)

Parameters

leftResource TResource
rightResourceIds ISet<IIdentifiable>
cancellationToken CancellationToken

Returns

Task

SaveChangesAsync(CancellationToken)

protected virtual Task SaveChangesAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Returns

Task

SetRelationshipAsync(TResource, object?, CancellationToken)

Performs a complete replacement of the relationship in the underlying data store.

public virtual Task SetRelationshipAsync(TResource leftResource, object? rightValue, CancellationToken cancellationToken)

Parameters

leftResource TResource
rightValue object
cancellationToken CancellationToken

Returns

Task

UpdateAsync(TResource, TResource, CancellationToken)

Updates the attributes and relationships of an existing resource in the underlying data store.

public virtual Task UpdateAsync(TResource resourceFromRequest, TResource resourceFromDatabase, CancellationToken cancellationToken)

Parameters

resourceFromRequest TResource
resourceFromDatabase TResource
cancellationToken CancellationToken

Returns

Task

UpdateRelationshipAsync(RelationshipAttribute, TResource, object?, CancellationToken)

protected Task UpdateRelationshipAsync(RelationshipAttribute relationship, TResource leftResource, object? valueToAssign, CancellationToken cancellationToken)

Parameters

relationship RelationshipAttribute
leftResource TResource
valueToAssign object
cancellationToken CancellationToken

Returns

Task