using CanDoo.Contracts;using CanDoo.Core.Data;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using CanDoo.Data;using System.Linq.Expressions;using CanDoo.Extensions;using CanDoo.Core.Caching;using Mehdime.Entity;using System.Data.Entity;using System.Linq.Dynamic;using CanDoo.Core.Data.Extensions;using CanDoo.Filter;using CanDoo.Exceptions;using System.Diagnostics.Contracts;namespace CanDoo.Data.Entity.Services{ public abstract class ServiceBase : ServiceBase where TEntity : class, IEntity ,IEntity, new() { } public abstract class ServiceBase : IServiceBase where TEntity : class, IEntity , new() { #region 属性 public virtual IQueryable Entities { get { return Repository.Entities; } } public IRepository Repository { get; set; } /// /// 很快要删除此属性 /// private IUnitOfWork UnitOfWork { get { return Repository.UnitOfWork; } } #endregion public IDbContextScopeFactory DbScopeFactory { get; set; } public ServiceBase() { OnDeleting += BeforeDelete; OnDeleted += AfterDelete; OnUpdating += BeforeUpdate; OnUpdated += AfterUpdate; OnAdding += BeforeAdd; OnAdded += AfterAdd; OnQuerying += AfterBeforeQuery; OnQueried += AfterQuery; } /// /// 这个改成内部方法,,读出来总要在操作单元(事务中使用,不然读出来也无法惰性加载 /// /// /// public TEntity GetById(TKey Id) { return Repository.GetByKey(Id); } /// /// 根据id读出只读实体,在action内,实体可以惰性加载 /// /// /// public void GetById(TKey Id, Action readOnlyAcion) { using (var scope = DbScopeFactory.CreateReadOnly()) { TEntity entity = GetById(Id); readOnlyAcion(entity); } } /// /// 根据id读出实体,在action内,实体可以惰性加载,如果查不数据,new一个对象出来 /// /// /// public void LoadById(TKey Id, Action readOnlyLoadAction) { using (var scope = DbScopeFactory.CreateReadOnly()) { TEntity entity = GetById(Id); if (entity == null) { entity = new TEntity(); entity.Id = Id; } readOnlyLoadAction(entity); } } #region GetPaged public PageResult GetPaged (Expression > selector, System.Linq.Expressions.Expression > filter = null, string sortExpression = "", int pagesize = 10, int pageindex = 0, string includeProperties = "") { using (var dbContextScope = DbScopeFactory.CreateReadOnly()) { var query = Entities; if (filter != null) { query = query.Where(filter); } if (!string.IsNullOrEmpty(includeProperties)) { foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } } if (!string.IsNullOrEmpty(sortExpression)) { query = query.SortBy(sortExpression); } else { query = query.SortBy("Id desc"); } var selectedQuery = query.Select(selector); selectedQuery = selectedQuery.Skip(pagesize * pageindex).Take(pagesize); var data = selectedQuery.ToArray(); //Entities.Where(filter).OrderBy(p => p.Id).Skip(pagesize * pageindex).Take(pagesize).ToArray(); return new PageResult { Data = data, Total = this.Repository.Count(filter) }; } } public PageResult GetPaged (System.Linq.Expressions.Expression > filter = null, Expression > selector = null, Func , IOrderedQueryable > orderBy = null, int pagesize = 10, int pageindex = 0, string includeProperties = "") { //using (var dbContextScope = _dbContextScopeFactory.Create()) { IQueryable query = this.Entities; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } IQueryable qResult = query.Select(selector); IOrderedQueryable qSort; if (orderBy != null) { qSort = orderBy(qResult); qResult = qSort.Skip(pagesize * pageindex).Take(pagesize); } else { qResult = qResult.Skip(pagesize * pageindex).Take(pagesize); } return new PageResult { Data = qResult.ToArray(), Total = query.Count() }; } } public IQueryable Get(System.Linq.Expressions.Expression > filter = null, Func , IOrderedQueryable > orderBy = null, string includeProperties = "") { //q.Skip(GridList.PageIndex * GridList.PageSize).Take(GridList.PageSize).ToList() IQueryable query = this.Entities; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { return orderBy(query); } else { return query; } } #endregion #region 增删改查 #region Add public OperationResult AddEntity(TEntity Entity) { Entity.CheckNotNull("Entity"); using (var dbContextScope = DbScopeFactory.Create()) { var result = OperationResult.NoChanged; if (OnAdding != null) { result = BeforeAdd(new[] { Entity.Id }, new[] { Entity }); if (result.ResultType != OperationResultType.NoChanged && result.ResultType != OperationResultType.Success) return result; } Repository.Insert(Entity);// try { result = dbContextScope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged; } catch (Exception ex) { result = new OperationResult(OperationResultType.Error, ex.Message); } //只有操作成功才执行AfterAdd事件 if (result.ResultType == OperationResultType.Success && OnAdded != null) { AfterAdd(new[] { Entity.Id }, new[] { Entity }); } return result; } } public OperationResult AddEntity(IEnumerable entities) { entities.CheckNotNull("entities"); using (var dbContextScope = DbScopeFactory.Create()) { Repository.Insert(entities); return dbContextScope.SaveChanges() > 0 ? new OperationResult(OperationResultType.Success, "数据添加成功!") : new OperationResult(OperationResultType.NoChanged); } } public OperationResult AddEntity(TKey id, Func addFunc) { using (var scope = DbScopeFactory.Create()) { var result = OperationResult.NoChanged; try { TEntity entity = new TEntity(); entity.Id = id; result = addFunc(entity); if (result.ResultType == OperationResultType.Success) { //执行添加事件 result = BeforeAdd?.Invoke(new[] { id }, new[] { entity }) ?? OperationResult.Success; if (result == null || result.ResultType == OperationResultType.Success) { Repository.Insert(entity); result = scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged; } } if (result.ResultType == OperationResultType.Success) { AfterAdd?.Invoke(new[] { id }, new[] { entity }); } } catch (Exception ex) { result = new OperationResult(OperationResultType.Error, ex.Message, ex); } return result; } } #endregion #region Delete public OperationResult DeleteEntity(TEntity Entity) { Entity.CheckNotNull("Entity"); return Repository.Delete(Entity) > 0 ? new OperationResult(OperationResultType.Success, "数据删除成功!") : new OperationResult(OperationResultType.NoChanged); } public OperationResult DeleteEntity(TKey id) { using (var dbContextScope = DbScopeFactory.Create()) { return Repository.Delete(id) > 0 ? new OperationResult(OperationResultType.Success, "数据删除成功!") : new OperationResult(OperationResultType.NoChanged); } } public OperationResult DeleteEntity(IEnumerable Entities) { Entities.CheckNotNull("Entities"); using (var dbContextScope = DbScopeFactory.Create()) { return Repository.Delete(Entities) > 0 ? new OperationResult(OperationResultType.Success, "数据删除成功!") : new OperationResult(OperationResultType.NoChanged); } } public OperationResult DeleteEntity(List ids) { ids.CheckNotNull("ids"); using (var dbContextScope = DbScopeFactory.Create()) { Repository.Delete(p => ids.Contains(p.Id)); return dbContextScope.SaveChanges() > 0 ? new OperationResult(OperationResultType.Success, "数据删除成功!") : new OperationResult(OperationResultType.NoChanged); } } public OperationResult DeleteEntity(Expression > predicate) { using (var dbContextScope = DbScopeFactory.Create()) { return Repository.Delete(predicate) > 0 ? new OperationResult(OperationResultType.Success, "数据删除成功!") : new OperationResult(OperationResultType.NoChanged); } } #endregion #region Update public OperationResult UpdateEntity(IEnumerable Entities) { Entities.CheckNotNull("Entities"); using (var dbContextScope = DbScopeFactory.Create()) { UnitOfWork.TransactionEnabled = true; foreach (var item in Entities) { Repository.Update(item); } return UnitOfWork.SaveChanges() > 0 ? new OperationResult(OperationResultType.Success, "数据更新成功!") : new OperationResult(OperationResultType.NoChanged); } } public OperationResult UpdateEntity(TEntity Entity) { Entity.CheckNotNull("Entity"); using (var dbContextScope = DbScopeFactory.Create()) { return Repository.Update(Entity) > 0 ? new OperationResult(OperationResultType.Success, "数据更新成功!") : new OperationResult(OperationResultType.NoChanged); } } public OperationResult UpdateEntity(TKey id, Func updateFunc) { using (var scope = DbScopeFactory.Create()) { var result = OperationResult.NoChanged; try { TEntity entity = GetById(id); if (entity == null) { return new OperationResult(OperationResultType.QueryNull, "你要更新的数据找不到,可能已经被删除。"); } result = updateFunc(entity); if (result.ResultType == OperationResultType.Success) { //执行更新前事件 result = BeforeUpdate?.Invoke(new[] { id }, new[] { entity }) ?? OperationResult.Success; ; if (result.ResultType == OperationResultType.Success) //Repository.Update(entity); 这个不需要了,对象已经在被跟踪状态 result = scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged; } if (result.ResultType == OperationResultType.Success) { AfterUpdate?.Invoke(new[] { id }, new[] { entity }); } } catch (Exception ex) { result = new OperationResult(OperationResultType.Error,ex.Message,ex); } return result; } } #endregion #endregion #region AttachEntity public TEntity AttachEntity(TEntity entity) { return this.Repository.AttachEntity(entity); } public void AttachEntity(ICollection existItems, TKey[] newItemIDs) { throw new NotImplementedException(); } public TEntity AttachEntityByKey(TKey keyID) { TEntity entity = new TEntity(); entity.Id = keyID; return AttachEntity(entity); } /// /// 更新原集合中的元素 /// /// /// /// /// protected void ReplaceEntity (ICollection orignEntities, ICollection newEntities) { //删除旧的 orignEntities.Where(p => newEntities.Contains(p) == false).ToList().ForEach(deleted => orignEntities.Remove(deleted)); //添加新的 newEntities.Where(p => orignEntities.Contains(p) == false).ToList().ForEach(added => orignEntities.Add(added)); } #endregion #region Event public event Func , IEnumerable , OperationResult> OnAdded; public event Func , IEnumerable , OperationResult> OnAdding; public event Func , IEnumerable , OperationResult> OnDeleted; public event Func , IEnumerable , OperationResult> OnDeleting; public event Func , IEnumerable , OperationResult> OnQueried; public event Func , IEnumerable , OperationResult> OnQuerying; public event Func , IEnumerable , OperationResult> OnUpdated; public event Func , IEnumerable , OperationResult> OnUpdating; #endregion #region Event Handler 事件处理器 public Func , IEnumerable , OperationResult> AfterAdd; public Func , IEnumerable , OperationResult> AfterBeforeQuery; public Func , IEnumerable , OperationResult> AfterDelete; public Func , IEnumerable , OperationResult> AfterQuery; public Func , IEnumerable , OperationResult> AfterUpdate; public Func , IEnumerable , OperationResult> BeforeAdd; public Func , IEnumerable , OperationResult> BeforeDelete; public Func , IEnumerable , OperationResult> BeforeUpdate; #endregion #region 事务操作 public void KeepReading(Action readFromeDbOnlyAction) { using (var dbContextScope = DbScopeFactory.CreateReadOnly()) { readFromeDbOnlyAction(); } } public OperationResult Save(Func saveAction) { using (var dbContextScope = DbScopeFactory.Create()) { OperationResult or = OperationResult.NoChanged; #region 执行保存过程 or = saveAction?.Invoke(); if (or.ResultType != OperationResultType.Success && or.ResultType != OperationResultType.NoChanged) { return or; } #endregion try { or = dbContextScope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged; } catch (CanDooException ex) { or = new OperationResult(OperationResultType.Error, ex.Message); } return or; } } #endregion }}