Generation

generate functionThu, 22 Dec 2022

Create a generic repository pattern-based class which exposes all the CRUD methods for handling a specific entity

public class Repository<T> : IRepository<T> where T : class { private DbContext _context; private DbSet<T> _dbSet; public Repository() { _context = new DbContext(); _dbSet = _context.Set<T>(); } public void Delete(object id) { T entityToDelete = _dbSet.Find(id); Delete(entityToDelete); } public void Delete(T entityToDelete) { if (_context.Entry(entityToDelete).State == EntityState.Detached) { _dbSet.Attach(entityToDelete); } _dbSet.Remove(entityToDelete); } public IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "") { IQueryable<T>

Questions about programming?Chat with your personal AI assistant