INTERFACES
public interface IRepository where T : BaseEntity, new()
{
public Task AddAsync(T entity);
public Task> GetAllAsync();
public Task GetByIdAsync(int Id);
public Task GetByConditionAsync(Expression> condition);
public void Delete(T entity);
public void Update(T entity);
public Task SaveChangesAsync();
}
IMPLEMENTATIONS
public class Repository : IRepository where T : BaseEntity, new()
{
readonly AppDbContext _context;
public Repository(AppDbContext context)
{
_context = context;
}
DbSet<T> Table => _context.Set<T>();
public async Task AddAsync(T entity)
{
await Table.AddAsync(entity);
}
public void Delete(T entity)
{
Table.Remove(entity);
}
public async Task<ICollection<T>> GetAllAsync()
{
return await Table.ToListAsync();
}
public async Task<T> GetByConditionAsync(Expression<Func<T, bool>> condition)
{
return await Table.FirstOrDefaultAsync(condition);
}
public async Task<T> GetByIdAsync(int Id)
{
return await Table.FirstOrDefaultAsync(e => e.Id == Id);
}
public async Task<int> SaveChangesAsync()
{
return await _context.SaveChangesAsync();
}
public void Update(T entity)
{
Table.Update(entity);
}
}