profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

main default branch 429 files Expires Sep 13, 2026, 9:06 AM
BaseRepository.cs 1,099 bytes
1 using Base.Contracts;
2 using Microsoft.EntityFrameworkCore;
3
4 namespace App.DAL.EF.Repositories;
5
6 public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, IBaseEntity
7 {
8 protected readonly AppDbContext DbContext;
9 protected readonly DbSet<TEntity> DbSet;
10
11 public BaseRepository(AppDbContext dbContext)
12 {
13 DbContext = dbContext;
14 DbSet = dbContext.Set<TEntity>();
15 }
16
17 public virtual async Task<IEnumerable<TEntity>> GetAllAsync() => await DbSet.ToListAsync();
18 public virtual async Task<TEntity?> GetByIdAsync(Guid id) => await DbSet.FirstOrDefaultAsync(e => e.Id == id);
19 public virtual TEntity Add(TEntity entity) => DbSet.Add(entity).Entity;
20 public virtual TEntity Update(TEntity entity) => DbSet.Update(entity).Entity;
21 public virtual async Task<TEntity?> RemoveAsync(Guid id)
22 {
23 var entity = await GetByIdAsync(id);
24 if (entity == null) return null;
25 return DbSet.Remove(entity).Entity;
26 }
27 public virtual async Task<bool> ExistsAsync(Guid id) => await DbSet.AnyAsync(e => e.Id == id);
28 }
29