Last active
November 14, 2025 18:48
-
-
Save DhruvSavaj/3f84d2f17cd30ea6e6eb832a44bbe818 to your computer and use it in GitHub Desktop.
Simple Common Repository for CRUD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //IBaseRepository.cs | |
| public interface IBaseRepository<T> where T : BaseEntity | |
| { | |
| Task<T?> GetByIdAsync(int id); | |
| Task<IEnumerable<T>> GetAllAsync(); | |
| Task<T> AddAsync(T entity); | |
| Task<T?> UpdateAsync(T entity); | |
| Task<bool> DeleteAsync(int id); | |
| Task<IEnumerable<T>> SearchAsync(params Expression<Func<T, bool>>[] predicate); | |
| } | |
| //BaseRepository.cs | |
| public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity | |
| { | |
| protected readonly YourContext _context; | |
| private readonly DbSet<T> _dbSet; | |
| public BaseRepository(YourContext context) | |
| { | |
| _context = context; | |
| _dbSet = _context.Set<T>(); | |
| } | |
| public async Task<IEnumerable<T>> GetAllAsync() => await _dbSet.Where(x => !x.IsDeleted).ToListAsync(); | |
| public async Task<T?> GetByIdAsync(int id) => await _dbSet.FirstOrDefaultAsync(x => x.Id == id && !x.IsDeleted); | |
| public async Task<IEnumerable<T>> SearchAsync(params Expression<Func<T, bool>>[] predicate) | |
| { | |
| IQueryable<T> query = _dbSet; | |
| query = query.Where(x => !x.IsDeleted); | |
| foreach (var item in predicate) | |
| { | |
| query = query.Where(item); | |
| } | |
| return await query.ToListAsync(); | |
| } | |
| public async Task<T> AddAsync(T entity) | |
| { | |
| entity.Id = 0; | |
| entity.CreatedAt = DateTime.UtcNow; | |
| await _dbSet.AddAsync(entity); | |
| await SaveChanges(); | |
| return entity; | |
| } | |
| public async Task<T?> UpdateAsync(T entity) | |
| { | |
| if (entity is null) | |
| return null; | |
| entity.UpdatedAt = DateTime.UtcNow; | |
| _dbSet.Update(entity); | |
| await SaveChanges(); | |
| return entity; | |
| } | |
| public async Task<bool> DeleteAsync(int id) | |
| { | |
| var entityToDelete = await GetByIdAsync(id); | |
| if (entityToDelete is not null) | |
| { | |
| entityToDelete.IsDeleted = true; | |
| entityToDelete.DeletedAt = DateTime.Now; | |
| _dbSet.Update(entityToDelete); | |
| return await SaveChanges() > 0; | |
| } | |
| return false; | |
| } | |
| private async Task<int> SaveChanges() => await _context.SaveChangesAsync(); | |
| } | |
| // Example of how to consume base repository | |
| public class AccountService : IAccountRepository | |
| { | |
| readonly IBaseRepository<Account> _accountRepository; | |
| public AccountService(IBaseRepository<Account> accountRepository) | |
| { | |
| _accountRepository = accountRepository; | |
| } | |
| public async Task<Account?> Get(int id) | |
| { | |
| var account = await _accountRepository.GetByIdAsync(id); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment