123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using DW5S.Entity;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.EntityFrameworkCore.ChangeTracking;
- 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> where T : BaseEntity
- {
- 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<T> AddOrUpdateAsync(T entity);
- Task AddOrUpdateAsync(IEnumerable<T> entitis);
- /// <summary>
- /// 修改某些字段
- /// </summary>
- /// <typeparam name="TProperty"></typeparam>
- /// <param name="predicate"></param>
- /// <param name="propertyExpression"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- Task<int> UpdatePropAsync<TProperty>(Expression<Func<T, bool>> predicate, Func<T, TProperty> propertyExpression, TProperty value);
- Task<int> DeleteAsync(T entity);
- Task<int> DeleteAsync(IEnumerable<T> entitis);
- Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null);
- Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null);
- T MaxBy<TResult>(Expression<Func<T, TResult>> selector, Expression<Func<T, bool>> predicate = null);
- }
- public class Repository<T> : IRepository<T> where T : BaseEntity
- {
- 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 AddOrUpdateAsync(IEnumerable<T> entity)
- {
- if (entity.First().Id == 0)
- {
- await dbSet.AddRangeAsync(entity);
- }
- else
- {
- dbSet.UpdateRange(entity);
- }
- }
- public async Task<T> AddOrUpdateAsync(T entity)
- {
- if (entity.Id == 0)
- {
- var res = await dbSet.AddAsync(entity);
- return res.Entity;
- }
- else
- {
- var res = dbSet.Update(entity);
- return res.Entity;
- }
- }
- public async Task<int> DeleteAsync(T entity)
- {
- return await dbSet.Where(p => p.Id == entity.Id).ExecuteDeleteAsync();
- }
- public async Task<int> DeleteAsync(IEnumerable<T> entities)
- {
- var ids = entities.Select(p => p.Id);
- return await DeleteAsync(p => ids.Contains(p.Id));
- }
- 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);
- }
- public T MaxBy<TResult>(Expression<Func<T, TResult>> selector, Expression<Func<T, bool>> predicate = null)
- {
- if (predicate == null)
- return dbSet.MaxBy(selector);
- else
- return dbSet.Where(predicate).MaxBy(selector);
- }
-
- public async Task<int> UpdatePropAsync<TProperty>(Expression<Func<T, bool>> predicate, Func<T, TProperty> propertyExpression, TProperty value)
- {
- return await dbSet.Where(predicate)
- .ExecuteUpdateAsync(setters => setters.SetProperty(propertyExpression, value));
- }
- }
- }
|