Skip to content

IRepository<TEntity>

Interface public
Defines a generic repository contract for performing CRUD and query operations against a persistent store of TEntity objects.
Namespace SampleApi
Assembly SampleApi
Package v1.0.0.0
public interface IRepository<TEntity>
where TEntity : class
TEntity class
TEntity
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.
IRepository`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 CancellationToken to all I/O calls.
AddAsync Section titled AddAsync abstract
Task<TEntity>
Task<TEntity> IRepository<TEntity>.AddAsync(
TEntity entity,
CancellationToken cancellationToken = default(CancellationToken))

Adds a new entity to the repository.

entity TEntity
The entity to add.
cancellationToken CancellationToken optional
A token to cancel the asynchronous operation.
Task<TEntity> The added entity, including any server-generated values.
CountAsync Section titled CountAsync abstract
Task<int>
Task<int> IRepository<TEntity>.CountAsync(
CancellationToken cancellationToken = default(CancellationToken))

Returns the total number of entities in the repository.

cancellationToken CancellationToken optional
A token to cancel the asynchronous operation.
Task<int> The total count of entities.
DeleteAsync Section titled DeleteAsync abstract
Task<bool>
Task<bool> IRepository<TEntity>.DeleteAsync(
Guid id,
CancellationToken cancellationToken = default(CancellationToken))

Removes an entity from the repository by its identifier.

id Guid
The unique identifier of the entity to remove.
cancellationToken CancellationToken optional
A token to cancel the asynchronous operation.
Task<bool> true if the entity was removed; otherwise, false.
ExistsAsync Section titled ExistsAsync abstract
Task<bool>
Task<bool> IRepository<TEntity>.ExistsAsync(
Guid id,
CancellationToken cancellationToken = default(CancellationToken))

Determines whether an entity with the specified identifier exists.

id Guid
The unique identifier to check.
cancellationToken CancellationToken optional
A token to cancel the asynchronous operation.
Task<bool> true if an entity with the given identifier exists; otherwise, false.
GetAllAsync Section titled GetAllAsync abstract
Task<IReadOnlyList<TEntity>>
Task<IReadOnlyList<TEntity>> IRepository<TEntity>.GetAllAsync(
CancellationToken cancellationToken = default(CancellationToken))

Retrieves all entities in the repository.

cancellationToken CancellationToken optional
A token to cancel the asynchronous operation.
Task<IReadOnlyList<TEntity>> A read-only list of all entities.
GetByIdAsync Section titled GetByIdAsync abstract
Task<TEntity?>
Task<TEntity?> IRepository<TEntity>.GetByIdAsync(
Guid id,
CancellationToken cancellationToken = default(CancellationToken))

Retrieves an entity by its unique identifier.

id Guid
The unique identifier of the entity.
cancellationToken CancellationToken optional
A token to cancel the asynchronous operation.
Task<TEntity?> The entity if found; otherwise, null.
GetPagedAsync Section titled GetPagedAsync abstract
Task<PagedResult<TEntity>>
Task<PagedResult<TEntity>> IRepository<TEntity>.GetPagedAsync(
int page,
int pageSize,
CancellationToken cancellationToken = default(CancellationToken))

Retrieves a paged subset of entities.

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.
Task<PagedResult<TEntity>> A PagedResult`1 containing the requested page of entities.
UpdateAsync Section titled UpdateAsync abstract
Task<TEntity>
Task<TEntity> IRepository<TEntity>.UpdateAsync(
TEntity entity,
CancellationToken cancellationToken = default(CancellationToken))

Updates an existing entity in the repository.

entity TEntity
The entity with updated values.
cancellationToken CancellationToken optional
A token to cancel the asynchronous operation.
Task<TEntity> The updated entity.

Using 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);
}