A generic repository interface for .NET applications implementing the repository pattern.
- Generic
IRepository<TEntity>interface for data access abstraction - Async/await support with
CancellationToken - LINQ-based querying with
IQueryable<T> - Extension methods for pagination and common operations
- No-tracking queries for read-only scenarios
dotnet add package UkadGroup.IRepositoryOr via NuGet Package Manager:
Install-Package UkadGroup.IRepository
Implement the interface for your data access layer:
public class EfRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly DbContext _context;
public IQueryable<TEntity> GetAll() => _context.Set<TEntity>();
public async Task<TEntity> GetByIdAsync(int id, CancellationToken ct = default)
=> await _context.Set<TEntity>().FindAsync(new object[] { id }, ct);
// ... implement other methods
}Use with dependency injection:
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));| Method | Description |
|---|---|
GetAll() |
Returns all entities as queryable |
GetAllAsNoTracking() |
Returns entities without change tracking |
GetByIdAsync() |
Finds entity by ID |
AddAsync() |
Adds new entity |
Update() |
Updates existing entity |
Delete() |
Removes entity |
SaveChangesAsync() |
Persists changes |
MIT License - see LICENSE for details.
UKAD provides engineering support for teams that need help with solution design, integration or long term development. If you want to extend this project or need expertise in .NET, React, Azure, AI, Umbraco or Optimizely, you can contact us at hi@ukad-group.com.