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
grantStoreIGrantStoreThe provider-neutral grant store.
loggerILogger<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
aggregateGrantThe aggregate to delete.
cancellationTokenCancellationTokenThe 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
aggregateIdstringThe aggregate identifier.
cancellationTokenCancellationTokenThe cancellation token.
Returns
GetByIdAsync(string, CancellationToken)
Gets an aggregate by its strongly-typed identifier.
public Task<Grant?> GetByIdAsync(string aggregateId, CancellationToken cancellationToken)
Parameters
aggregateIdstringThe aggregate identifier.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<Grant>
The aggregate if found; null if no stream exists for the identifier; or an aggregate in its initial state --
Version0, 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
scopeGrantScopeThe grant scope to filter results.
userIdstringOptional user ID to narrow the search.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<IEnumerable<Grant>>
An IEnumerable<T> of Grant matching the criteria.
Exceptions
- ArgumentNullException
Thrown if
scopeis null.- ArgumentException
Thrown if
userIdis null or empty.
ReadAllAsync(string, CancellationToken)
Reads all grants associated with a specific user.
public Task<IEnumerable<Grant>> ReadAllAsync(string userId, CancellationToken cancellationToken)
Parameters
userIdstringThe user ID for which to retrieve grants.
cancellationTokenCancellationTokenA token to cancel the asynchronous operation.
Returns
- Task<IEnumerable<Grant>>
An IEnumerable<T> of Grant for the user.
Exceptions
- ArgumentException
Thrown if
userIdis 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
aggregateGrantThe aggregate to save.
expectedETagstringThe expected ETag value. If the current ETag doesn't match, a ConcurrencyException is thrown. Pass null to skip ETag validation.
cancellationTokenCancellationTokenThe 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
aggregateGrantThe aggregate to save.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<SaveResult>
A task that represents the asynchronous save operation.