Table of Contents

Class GrantRepository

Namespace
Excalibur.A3.Authorization.Grants
Assembly
Excalibur.A3.dll

Represents a repository for managing Grant entities, providing CRUD operations and utility methods for handling grants.

public class GrantRepository : IGrantRepository, IEventSourcedRepository<Grant>, IEventSourcedRepository<Grant, string>
Inheritance
GrantRepository
Implements
Inherited Members

Remarks

This repository implements the IEventSourcedRepository<TAggregate> pattern from Excalibur.EventSourcing while delegating persistence to an IGrantStore implementation.

The Grant aggregate uses event sourcing for in-memory state management but persists the current state snapshot via the provider-neutral store interface.

Constructors

GrantRepository(IGrantStore, ILogger<GrantRepository>)

Initializes a new instance of the GrantRepository class.

public GrantRepository(IGrantStore grantStore, ILogger<GrantRepository> logger)

Parameters

grantStore IGrantStore

The provider-neutral grant store.

logger ILogger<GrantRepository>

Logger for logging messages and errors.

Methods

DeleteAsync(Grant, CancellationToken)

Soft-deletes an aggregate by appending a tombstone event.

public Task DeleteAsync(Grant aggregate, CancellationToken cancellationToken)

Parameters

aggregate Grant

The aggregate to delete.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

A task that represents the asynchronous delete operation.

Remarks

This performs a soft delete by marking the aggregate as deleted. The aggregate's event history is preserved for audit purposes.

ExistsAsync(string, CancellationToken)

Checks if an aggregate exists.

public Task<bool> ExistsAsync(string aggregateId, CancellationToken cancellationToken)

Parameters

aggregateId string

The aggregate identifier.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<bool>

true if the aggregate exists; otherwise, false.

GetByIdAsync(string, CancellationToken)

Gets an aggregate by its strongly-typed identifier.

public Task<Grant?> GetByIdAsync(string aggregateId, CancellationToken cancellationToken)

Parameters

aggregateId string

The aggregate identifier.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<Grant>

The aggregate if found; null if no stream exists for the identifier; or an aggregate in its initial state -- Version 0, no history applied -- if the stream exists but has been erased. The erased case is deliberately distinct from both: it is not null, because the subject did exist, and it does not throw, because the erasure was lawful rather than a corruption. A caller that only null-checks will treat an erased stream as a live aggregate that happens to have no history.

MatchingAsync(GrantScope, string?, CancellationToken)

Retrieves grants matching a specific GrantScope and optional user ID.

public Task<IEnumerable<Grant>> MatchingAsync(GrantScope scope, string? userId, CancellationToken cancellationToken)

Parameters

scope GrantScope

The grant scope to filter results.

userId string

Optional user ID to narrow the search.

cancellationToken CancellationToken

A token to cancel the asynchronous operation.

Returns

Task<IEnumerable<Grant>>

An IEnumerable<T> of Grant matching the criteria.

Exceptions

ArgumentNullException

Thrown if scope is null.

ArgumentException

Thrown if userId is null or empty.

ReadAllAsync(string, CancellationToken)

Reads all grants associated with a specific user.

public Task<IEnumerable<Grant>> ReadAllAsync(string userId, CancellationToken cancellationToken)

Parameters

userId string

The user ID for which to retrieve grants.

cancellationToken CancellationToken

A token to cancel the asynchronous operation.

Returns

Task<IEnumerable<Grant>>

An IEnumerable<T> of Grant for the user.

Exceptions

ArgumentException

Thrown if userId is null or empty.

SaveAsync(Grant, string?, CancellationToken)

Saves an aggregate with ETag-based optimistic concurrency check.

public Task<SaveResult> SaveAsync(Grant aggregate, string? expectedETag, CancellationToken cancellationToken)

Parameters

aggregate Grant

The aggregate to save.

expectedETag string

The expected ETag value. If the current ETag doesn't match, a ConcurrencyException is thrown. Pass null to skip ETag validation.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<SaveResult>

A task that represents the asynchronous save operation.

Exceptions

ConcurrencyException

Thrown when the expected ETag doesn't match the current aggregate ETag.

SaveAsync(Grant, CancellationToken)

Saves an aggregate and its uncommitted events.

public Task<SaveResult> SaveAsync(Grant aggregate, CancellationToken cancellationToken)

Parameters

aggregate Grant

The aggregate to save.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<SaveResult>

A task that represents the asynchronous save operation.