Add Default CURD impl

This commit is contained in:
lihanbo 2024-10-16 11:27:57 +08:00
parent 0d5dc03030
commit 215540ca1c
2 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,40 @@
using System;
using System.Reflection;
using LFlow.Base.Interfaces;
namespace LFlow.Base.Default;
/// <summary>
/// 默认的CURD操作
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="K"></typeparam>
public abstract class DefaultCurd<T, K> where T : IModel, IDataModel
{
private readonly IRepo<T, K> _repo;
public DefaultCurd(IRepo<T, K> 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<T> GetAll(int pageIndex, int pageSize, ref int pageTotal)
{
return _repo.GetAll(pageIndex, pageSize, ref pageTotal);
}
public abstract List<T> GetWhere(T WhereObject);
}

View File

@ -0,0 +1,50 @@
using System;
using LFlow.Base.Interfaces;
using SqlSugar;
namespace LFlow.Base.Default;
public abstract class DefaultCurdRepo<T, K> : IRepo<T, K> 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<T>().In(id).ExecuteCommand();
}
else
{
throw new Exception("删除的对象不存在");
}
}
public virtual T Get(K id)
{
return _client.Queryable<T>().InSingle(id);
}
public List<T> GetAll(int pageIndex, int pageSize, ref int pageTotal)
{
return _client.Queryable<T>().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<T> Search(T whereObj);
public abstract List<K> WhereSearchId(T whereObj);
}