diff --git a/LFlow/Default/DefaultCurd.cs b/LFlow/Default/DefaultCurd.cs new file mode 100644 index 0000000..54e147b --- /dev/null +++ b/LFlow/Default/DefaultCurd.cs @@ -0,0 +1,40 @@ +using System; +using System.Reflection; +using LFlow.Base.Interfaces; + +namespace LFlow.Base.Default; +/// +/// 默认的CURD操作 +/// +/// +/// +public abstract class DefaultCurd where T : IModel, IDataModel +{ + private readonly IRepo _repo; + public DefaultCurd(IRepo repo) + { + _repo = repo; + } + public virtual T Add(T model) + { + return _repo.SaveOrUpdate(model, false); + } + public virtual int Delete(K id) + { + return _repo.Delete(id); + } + public virtual T Update(T model) + { + return _repo.SaveOrUpdate(model, true); + } + public virtual T Get(K id) + { + return _repo.Get(id); + } + public virtual List GetAll(int pageIndex, int pageSize, ref int pageTotal) + { + return _repo.GetAll(pageIndex, pageSize, ref pageTotal); + } + public abstract List GetWhere(T WhereObject); + +} diff --git a/LFlow/Default/DefaultCurdRepo.cs b/LFlow/Default/DefaultCurdRepo.cs new file mode 100644 index 0000000..b9ff24a --- /dev/null +++ b/LFlow/Default/DefaultCurdRepo.cs @@ -0,0 +1,50 @@ +using System; +using LFlow.Base.Interfaces; +using SqlSugar; + +namespace LFlow.Base.Default; + +public abstract class DefaultCurdRepo : IRepo where T : class, IDataModel, new() +{ + private readonly ISqlSugarClient _client; + public DefaultCurdRepo(ISqlSugarClient client) + { + _client = client; + } + public virtual int Delete(K id) + { + if (Get(id) != null) + { + return _client.Deleteable().In(id).ExecuteCommand(); + } + else + { + throw new Exception("删除的对象不存在"); + } + + } + public virtual T Get(K id) + { + return _client.Queryable().InSingle(id); + } + + public List GetAll(int pageIndex, int pageSize, ref int pageTotal) + { + return _client.Queryable().ToPageList(pageIndex, pageSize, ref pageTotal); + } + + public virtual T SaveOrUpdate(T entity, bool isUpdate) + { + if (isUpdate) + { + _client.Updateable(entity).ExecuteCommand(); + } + else + { + _client.Insertable(entity).ExecuteCommand(); + } + return entity; + } + public abstract List Search(T whereObj); + public abstract List WhereSearchId(T whereObj); +}