IRepository<TEntity>
Interface public
Defines a generic repository contract for performing CRUD and query operations against a persistent store of
TEntity objects. Definition
Section titled Definitionpublic interface IRepository<TEntity> where TEntity : classTEntity class Type Parameters
Section titled Type ParametersTEntity The entity type managed by this repository. Must be a reference type so that
null can be returned from CancellationToken) when an entity is not found.Remarks
Section titled RemarksIRepository`1 abstracts the data-access layer, allowing service classes to operate against an in-memory fake, an EF Core DbContext, or a Dapper-based implementation without code changes. Implementations are responsible for:- Opening and closing database connections or scoping units of work.
- Mapping between CLR entities and the underlying storage representation.
- Propagating
CancellationTokento all I/O calls.
Methods8
Section titled MethodsTask<TEntity> Task<TEntity> IRepository<TEntity>.AddAsync( TEntity entity, CancellationToken cancellationToken = default(CancellationToken))Adds a new entity to the repository.
Parameters
entity TEntity The entity to add.
cancellationToken CancellationToken optional A token to cancel the asynchronous operation.
Returns
Task<TEntity> The added entity, including any server-generated values. Task<int> Task<int> IRepository<TEntity>.CountAsync( CancellationToken cancellationToken = default(CancellationToken))Returns the total number of entities in the repository.
Parameters
cancellationToken CancellationToken optional A token to cancel the asynchronous operation.
Returns
Task<int> The total count of entities. Task<bool> Task<bool> IRepository<TEntity>.DeleteAsync( Guid id, CancellationToken cancellationToken = default(CancellationToken))Removes an entity from the repository by its identifier.
Parameters
id Guid The unique identifier of the entity to remove.
cancellationToken CancellationToken optional A token to cancel the asynchronous operation.
Returns
Task<bool> true if the entity was removed; otherwise, false. Task<bool> Task<bool> IRepository<TEntity>.ExistsAsync( Guid id, CancellationToken cancellationToken = default(CancellationToken))Determines whether an entity with the specified identifier exists.
Parameters
id Guid The unique identifier to check.
cancellationToken CancellationToken optional A token to cancel the asynchronous operation.
Returns
Task<bool> true if an entity with the given identifier exists; otherwise, false. Task<IReadOnlyList<TEntity>> Task<IReadOnlyList<TEntity>> IRepository<TEntity>.GetAllAsync( CancellationToken cancellationToken = default(CancellationToken))Retrieves all entities in the repository.
Parameters
cancellationToken CancellationToken optional A token to cancel the asynchronous operation.
Returns
Task<IReadOnlyList<TEntity>> A read-only list of all entities. Task<TEntity?> Task<TEntity?> IRepository<TEntity>.GetByIdAsync( Guid id, CancellationToken cancellationToken = default(CancellationToken))Retrieves an entity by its unique identifier.
Parameters
id Guid The unique identifier of the entity.
cancellationToken CancellationToken optional A token to cancel the asynchronous operation.
Returns
Task<TEntity?> The entity if found; otherwise, null. Task<PagedResult<TEntity>> Task<PagedResult<TEntity>> IRepository<TEntity>.GetPagedAsync( int page, int pageSize, CancellationToken cancellationToken = default(CancellationToken))Retrieves a paged subset of entities.
Parameters
page int The one-based page number to retrieve.
pageSize int The number of items per page.
cancellationToken CancellationToken optional A token to cancel the asynchronous operation.
Returns
Task<PagedResult<TEntity>> A PagedResult`1 containing the requested page of entities. Task<TEntity> Task<TEntity> IRepository<TEntity>.UpdateAsync( TEntity entity, CancellationToken cancellationToken = default(CancellationToken))Updates an existing entity in the repository.
Parameters
entity TEntity The entity with updated values.
cancellationToken CancellationToken optional A token to cancel the asynchronous operation.
Returns
Task<TEntity> The updated entity. Examples
Section titled ExamplesUsing the repository to page through all customers:
IRepository<Customer> repo = GetRepository();
var page = await repo.GetPagedAsync(page: 1, pageSize: 20);Console.WriteLine($"Showing {page.Items.Count} of {page.TotalCount}");
while (page.HasNextPage){ page = await repo.GetPagedAsync(page.Page + 1, pageSize: 20);}