Appearance
IRepository<T>
Generic repository interface for data access.
Methods
| Method | Return | Description |
|---|---|---|
| GetByIdAsync(Guid, CancellationToken) | Task<T?> | Get by ID |
| GetAllAsync(CancellationToken) | Task<IEnumerable<T>> | Get all entities |
| FindAsync(Expression<Func<T, bool>>, CancellationToken) | Task<IEnumerable<T>> | Find by predicate |
| FirstOrDefaultAsync(Expression<Func<T, bool>>, CancellationToken) | Task<T?> | First match or null |
| AnyAsync(Expression<Func<T, bool>>, CancellationToken) | Task<bool> | Check if any match |
| AddAsync(T, CancellationToken) | Task<T> | Add entity |
| UpdateAsync(T, CancellationToken) | Task<T> | Update entity |
| DeleteAsync(Guid, CancellationToken) | Task | Delete by ID |
| DeleteAsync(T, CancellationToken) | Task | Delete entity |
| ExistsAsync(Guid, CancellationToken) | Task<bool> | Check existence |
| CountAsync(Expression?, CancellationToken) | Task<int> | Count entities |
IRepository<T, TKey>
Use RepositoryBase<TEntity, TKey, TContext> when the entity key is not a Guid. EF Core must map the key type (with a value converter for custom value objects), and the key should implement Equals consistently with its database representation. GetByIdAsync compares keys in the query, so the provider can translate the comparison to SQL.
Usage
csharp
public readonly struct ProductKey(string value)
{
public string Value { get; } = value;
public override bool Equals(object? other)
=> other is ProductKey key && Value == key.Value;
public override int GetHashCode() => StringComparer.Ordinal.GetHashCode(Value);
}
public sealed class Product : IEntity<ProductKey>
{
public ProductKey Id { get; set; }
public DateTime CreatedAt { get; set; }
public bool IsDeleted { get; set; }
}
public sealed class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
public DbSet<Product> Products => Set<Product>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.Property(product => product.Id)
.HasConversion(key => key.Value, value => new ProductKey(value));
}
}
public sealed class ProductRepository(AppDbContext context)
: RepositoryBase<Product, ProductKey, AppDbContext>(context)
{
}GetByIdAsync, ExistsAsync, and DeleteAsync(id) use the normal EF query, so configured tenant and soft-delete query filters continue to apply. A tracked row hidden by either filter stays unavailable to these ID lookups. DeleteAsync only marks the entity for deletion in the current context; persist it by calling SaveChangesAsync on the context or IUnitOfWork after the delete.
Provider-backed CRUD queries
IRepository<TEntity> keeps its materializing read methods for compatibility. The optional IQueryRepository<TEntity> capability exposes QueryForCrud() for operations that need database filtering and pagination. The Guid-key RepositoryBase<TEntity, TContext> implements it through the protected QueryForCrudCore() hook, whose default starts from DbSet.AsNoTracking() and keeps EF global query filters.
If a custom Guid repository overrides GetAllAsync or FindAsync to enforce visibility, migrate those predicates into QueryForCrudCore() before using DefaultQueryOperation. The CRUD operation detects legacy interface dispatch, including explicit interface implementations, and throws before querying when the migration is missing. In an inheritance chain, the hook must be declared at or below every class that adds a visibility override. The returned query must retain an EF async provider; do not materialize it or switch to LINQ-to-Objects. Repositories that cannot provide such a query can implement IQueryRepository<TEntity> with equivalent filters or use a custom CRUD query operation. QueryNoTracking() is not adopted automatically because the old materializing reads did not call it.