1.7 KiB
1.7 KiB
📌 Repository Interfaces (Domain/Repositories)
namespace Domain.Repositories
{
public interface IRepository<T> where T : class
{
Task<T?> GetByIdAsync(int id);
Task<IEnumerable<T>> GetAllAsync();
Task AddAsync(T entity);
void Remove(T entity);
}
}
namespace Domain.Repositories
{
public interface IUnitOfWork
{
Task<int> CompleteAsync();
}
}
📌 Repository Pattern (Infrastructure/Persistence/Repositories)
using Domain.Repositories;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Persistence.Repositories
{
public class Repository<T> : IRepository<T> where T : class
{
protected readonly AppDbContext _context;
protected readonly DbSet<T> _dbSet;
public Repository(AppDbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public async Task<T?> GetByIdAsync(int id) => await _dbSet.FindAsync(id);
public async Task<IEnumerable<T>> GetAllAsync() => await _dbSet.ToListAsync();
public async Task AddAsync(T entity) => await _dbSet.AddAsync(entity);
public void Remove(T entity) => _dbSet.Remove(entity);
}
}
📌 Unit of Work Implementation
using Domain.Repositories;
using System.Threading.Tasks;
namespace Infrastructure.Persistence
{
public class UnitOfWork : IUnitOfWork
{
private readonly AppDbContext _context;
public UnitOfWork(AppDbContext context)
{
_context = context;
}
public async Task<int> CompleteAsync()
{
return await _context.SaveChangesAsync();
}
}
}