123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using DW5S.Entity;
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- namespace DW5S.Repostory
- {
- public interface IRepository<T>
- {
- IQueryable<T> AsQueryable();
- Task<T> GetByIdAsync(object id);
- Task<IEnumerable<T>> GetAllAsync();
- Task<IEnumerable<T>> GetAllAsync<TKey>(Expression<Func<T, TKey>> keySelector = null, bool asc = true);
- Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate);
- Task<IEnumerable<T>> FindAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector = null, bool asc = true);
- Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector);
- Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null);
- Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null);
- Task<T> AddAsync(T entity);
- Task AddRangeAsync(IEnumerable<T> entitis);
- void Update(T entity);
- void Delete(T entity);
- Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null);
- }
- public class Repository<T> : IRepository<T> where T : class
- {
- protected readonly OracleContext ctx;
- protected readonly DbSet<T> dbSet;
- public Repository(OracleContext ctx)
- {
- this.ctx = ctx;
- this.dbSet = ctx.Set<T>();
- }
- public IQueryable<T> AsQueryable()
- {
- return dbSet.AsQueryable();
- }
- public async Task<T> GetByIdAsync(object id)
- {
- return await dbSet.FindAsync(id);
- }
- public async Task<IEnumerable<T>> GetAllAsync()
- {
- return await dbSet.ToListAsync();
- }
- public async Task<IEnumerable<T>> GetAllAsync<TKey>(Expression<Func<T, TKey>> keySelector = null, bool asc = true)
- {
- if (keySelector == null)
- {
- return await dbSet.ToListAsync();
- }
- else
- {
- if (asc)
- return await dbSet.OrderBy(keySelector).ToListAsync();
- else
- return await dbSet.OrderByDescending(keySelector).ToListAsync();
- }
- }
- public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
- {
- return await dbSet.Where(predicate).ToListAsync();
- }
- public async Task<IEnumerable<T>> FindAsync<TKey>(Expression<Func<T, bool>> predicate,Expression<Func<T, TKey>> keySelector = null, bool asc = true)
- {
- if (keySelector == null)
- {
- return await dbSet.Where(predicate).ToListAsync();
- }
- else
- {
- if (asc)
- return await dbSet.Where(predicate).OrderBy(keySelector).ToListAsync();
- else
- return await dbSet.Where(predicate).OrderByDescending(keySelector).ToListAsync();
- }
- }
- public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null)
- {
- if (predicate != null)
- return await dbSet.Where(predicate).FirstOrDefaultAsync();
- return await dbSet.FirstOrDefaultAsync();
- }
- public async Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector)
- {
- var res = await dbSet.Select(selector).Distinct().ToListAsync();
- return res;
- }
- public async Task<T> AddAsync(T entity)
- {
- var res = await dbSet.AddAsync(entity);
- return res.Entity;
- }
- public async Task AddRangeAsync(IEnumerable<T> entitis)
- {
- await dbSet.AddRangeAsync(entitis);
- }
- public void Update(T entity)
- {
- dbSet.Update(entity);
- }
- public void Delete(T entity)
- {
- dbSet.Remove(entity);
- }
- ///// <summary>
- ///// 高性能的删除
- ///// </summary>
- ///// <param name="id">ids可以是int、long、IEnumerable<int>、IEnumerable<long>等类型</param>
- //public async void DeleteById(object id)
- //{
- // if (id == null) return;
- // string table = typeof(T).Name;
- // if (id is IEnumerable<long> ids1)
- // {
- // //如果ID是连续的,这里可以使用大于小于等条件进一步优化
- // string sql = $"delete from {table} where id in ({string.Join(",", ids1)})";
- // await ctx.ExecuteSqlAsync(sql);
- // }
- // else if (id is IEnumerable<int> ids2)
- // {
- // string sql = $"delete from {table} where id in ({string.Join(",", ids2)})";
- // await ctx.ExecuteSqlAsync(sql);
- // }
- // else
- // {
- // string sql = $"delete from {table} where id={id}";
- // await ctx.ExecuteSqlAsync(sql);
- // }
- //}
- public async Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null)
- {
- return await dbSet.Where(predicate).ExecuteDeleteAsync();
- }
- public async Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null)
- {
- return await dbSet.AnyAsync(predicate);
- }
- }
- }
|