Last active
August 2, 2020 08:17
-
-
Save PradeepLoganathan/e0b9f61c2ceb3392ee5fcf855990cfe5 to your computer and use it in GitHub Desktop.
GenericRepository - https://pradeeploganathan.com/patterns/repository-and-unit-of-work-pattern-asp-net-core-3-1/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class GenericRepository<T> : IGenericRepository<T> where T : class | |
| { | |
| protected readonly BookStoreDbContext _context; | |
| public GenericRepository(BookStoreDbContext context) | |
| { | |
| _context = context; | |
| } | |
| public async Task<T> Get(int id) | |
| { | |
| return await _context.Set<T>().FindAsync(id); | |
| } | |
| public async Task<IEnumerable<T>> GetAll() | |
| { | |
| return await _context.Set<T>().ToListAsync(); | |
| } | |
| public async Task Add(T entity) | |
| { | |
| await _context.Set<T>().AddAsync(entity); | |
| } | |
| public void Delete(T entity) | |
| { | |
| _context.Set<T>().Remove(entity); | |
| } | |
| public void Update(T entity) | |
| { | |
| _context.Set<T>().Update(entity); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment