Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class BaseRepository<T> : IRepository<T> where T : class
- {
- internal readonly TinRollContext context;
- public BaseRepository(TinRollContext context)
- {
- this.context = context;
- }
- public async Task<T> CreateAsync(T entity)
- {
- await context.AddAsync<T>(entity);
- await context.SaveChangesAsync();
- return entity;
- }
- public async Task<T> GetAsync(int id)
- {
- return await context.FindAsync<T>(id);
- }
- public async Task<IEnumerable<T>> GetAsync(
- Expression<Func<T, bool>> filter = null,
- Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
- string includeProperties = "")
- {
- IQueryable<T> query = context.Set<T>();
- if (filter != null)
- {
- query = query.Where(filter);
- }
- if (includeProperties != null)
- {
- foreach (var includeProperty in includeProperties.Split
- (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
- {
- query = query.Include(includeProperty);
- }
- }
- if (orderBy != null)
- {
- return await orderBy(query).ToListAsync();
- }
- else
- {
- return await query.ToListAsync();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement