Files
ASP.NET/02_ProjectOrientedSessions/Session03/Session 03 Document.md
T

78 lines
1.7 KiB
Markdown

### 📌 **Repository Interfaces (Domain/Repositories)**
```csharp
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);
}
}
```
```csharp
namespace Domain.Repositories
{
public interface IUnitOfWork
{
Task<int> CompleteAsync();
}
}
```
### 📌 **Repository Pattern (Infrastructure/Persistence/Repositories)**
```csharp
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**
```csharp
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();
}
}
}
```