Thursday, January 23, 2025

Duplicating Hankel plot from A&S

Abramowitz and Stegun has quite a...

Why all developers should adopt a safety-critical mindset

mechanisms, active-passive system designs, and...

Example Repositories

Programming LanguageExample Repositories


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);
}

}

Check out our other content

Check out other tags:

Most Popular Articles