using NFine.Code; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text.RegularExpressions; namespace NFine.Data { /// /// 仓储实现 /// public class RepositoryBase : IRepositoryBase, IDisposable { private NFineDbContext dbcontext = new NFineDbContext(); private DbTransaction dbTransaction { get; set; } public IRepositoryBase BeginTrans() { DbConnection dbConnection = ((IObjectContextAdapter)dbcontext).ObjectContext.Connection; if (dbConnection.State == ConnectionState.Closed) { dbConnection.Open(); } dbTransaction = dbConnection.BeginTransaction(); return this; } public int Commit() { try { var returnValue = dbcontext.SaveChanges(); if (dbTransaction != null) { dbTransaction.Commit(); } return returnValue; } catch (Exception) { if (dbTransaction != null) { this.dbTransaction.Rollback(); } throw; } finally { this.Dispose(); } } public void Dispose() { if (dbTransaction != null) { this.dbTransaction.Dispose(); } this.dbcontext.Dispose(); } public int Insert(TEntity entity) where TEntity : class { dbcontext.Entry(entity).State = EntityState.Added; return dbTransaction == null ? this.Commit() : 0; } public int Insert(List entitys) where TEntity : class { foreach (var entity in entitys) { dbcontext.Entry(entity).State = EntityState.Added; } return dbTransaction == null ? this.Commit() : 0; } public int Update(TEntity entity) where TEntity : class { dbcontext.Set().Attach(entity); PropertyInfo[] props = entity.GetType().GetProperties(); foreach (PropertyInfo prop in props) { if (prop.GetValue(entity, null) != null) { if (prop.GetValue(entity, null).ToString() == " ") dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null; dbcontext.Entry(entity).Property(prop.Name).IsModified = true; } } return dbTransaction == null ? this.Commit() : 0; } public int Delete(TEntity entity) where TEntity : class { dbcontext.Set().Attach(entity); dbcontext.Entry(entity).State = EntityState.Deleted; return dbTransaction == null ? this.Commit() : 0; } public int Delete(Expression> predicate) where TEntity : class { var entitys = dbcontext.Set().Where(predicate).ToList(); entitys.ForEach(m => dbcontext.Entry(m).State = EntityState.Deleted); return dbTransaction == null ? this.Commit() : 0; } public TEntity FindEntity(object keyValue) where TEntity : class { return dbcontext.Set().Find(keyValue); } public TEntity FindEntity(Expression> predicate) where TEntity : class { return dbcontext.Set().FirstOrDefault(predicate); } public IQueryable IQueryable() where TEntity : class { return dbcontext.Set(); } public IQueryable IQueryable(Expression> predicate) where TEntity : class { return dbcontext.Set().Where(predicate); } public List FindList(string strSql) where TEntity : class { return dbcontext.Database.SqlQuery(strSql).ToList(); } public List FindList(string strSql, DbParameter[] dbParameter) where TEntity : class { return dbcontext.Database.SqlQuery(strSql, dbParameter).ToList(); } public List FindList(Pagination pagination) where TEntity : class,new() { bool isAsc = pagination.sord.ToLower() == "asc" ? true : false; string[] _order = pagination.sidx.Split(','); MethodCallExpression resultExp = null; var tempData = dbcontext.Set().AsQueryable(); foreach (string item in _order) { string _orderPart = item; _orderPart = Regex.Replace(_orderPart, @"\s+", " "); string[] _orderArry = _orderPart.Split(' '); string _orderField = _orderArry[0]; bool sort = isAsc; if (_orderArry.Length == 2) { isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false; } var parameter = Expression.Parameter(typeof(TEntity), "t"); var property = typeof(TEntity).GetProperty(_orderField); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExp = Expression.Lambda(propertyAccess, parameter); resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp)); } tempData = tempData.Provider.CreateQuery(resultExp); pagination.records = tempData.Count(); tempData = tempData.Skip(pagination.rows * (pagination.page - 1)).Take(pagination.rows).AsQueryable(); return tempData.ToList(); } public List FindList(Expression> predicate, Pagination pagination) where TEntity : class,new() { bool isAsc = pagination.sord.ToLower() == "asc" ? true : false; string[] _order = pagination.sidx.Split(','); MethodCallExpression resultExp = null; var tempData = dbcontext.Set().Where(predicate); foreach (string item in _order) { string _orderPart = item; _orderPart = Regex.Replace(_orderPart, @"\s+", " "); string[] _orderArry = _orderPart.Split(' '); string _orderField = _orderArry[0]; bool sort = isAsc; if (_orderArry.Length == 2) { isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false; } var parameter = Expression.Parameter(typeof(TEntity), "t"); var property = typeof(TEntity).GetProperty(_orderField); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExp = Expression.Lambda(propertyAccess, parameter); resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp)); } tempData = tempData.Provider.CreateQuery(resultExp); pagination.records = tempData.Count(); tempData = tempData.Skip(pagination.rows * (pagination.page - 1)).Take(pagination.rows).AsQueryable(); return tempData.ToList(); } } }