mirror of
https://github.com/nsnail/NetAdmin.git
synced 2025-12-30 09:25:49 +08:00
wip: 🧠 初步的框架
This commit is contained in:
82
src/backend/NetAdmin.Application/Repositories/Repository.cs
Normal file
82
src/backend/NetAdmin.Application/Repositories/Repository.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using NetAdmin.Domain.Contexts;
|
||||
using NetAdmin.Domain.DbMaps.Dependency;
|
||||
|
||||
namespace NetAdmin.Application.Repositories;
|
||||
|
||||
/// <inheritdoc cref="IRepository{TEntity}" />
|
||||
public sealed class Repository<TEntity> : DefaultRepository<TEntity, long>, IRepository<TEntity>
|
||||
where TEntity : EntityBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Repository{TEntity}" /> class.
|
||||
/// </summary>
|
||||
public Repository(IFreeSql fSql, UnitOfWorkManager uowManger, ContextUserToken userToken) //
|
||||
: base(fSql, uowManger)
|
||||
{
|
||||
UserToken = userToken;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前上下文关联的用户
|
||||
/// </summary>
|
||||
public ContextUserToken UserToken { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 递归删除
|
||||
/// </summary>
|
||||
/// <param name="exp">exp</param>
|
||||
/// <param name="disableGlobalFilterNames">禁用全局过滤器名</param>
|
||||
public async Task<bool> DeleteRecursiveAsync( //
|
||||
Expression<Func<TEntity, bool>> exp, params string[] disableGlobalFilterNames)
|
||||
{
|
||||
_ = await Select.Where(exp)
|
||||
.DisableGlobalFilter(disableGlobalFilterNames)
|
||||
.AsTreeCte()
|
||||
.ToDelete()
|
||||
.ExecuteAffrowsAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获得Dto
|
||||
/// </summary>
|
||||
/// <param name="id">主键</param>
|
||||
public Task<TDto> GetAsync<TDto>(long id)
|
||||
{
|
||||
return Select.WhereDynamic(id).ToOneAsync<TDto>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件获取Dto
|
||||
/// </summary>
|
||||
public Task<TDto> GetAsync<TDto>(Expression<Func<TEntity, bool>> exp)
|
||||
{
|
||||
return Select.Where(exp).ToOneAsync<TDto>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件获取实体
|
||||
/// </summary>
|
||||
public Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> exp)
|
||||
{
|
||||
return Select.Where(exp).ToOneAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取分页列表
|
||||
/// </summary>
|
||||
/// <param name="dynamicFilterInfo">动态过滤器</param>
|
||||
/// <param name="page">页码</param>
|
||||
/// <param name="pageSize">页容量</param>
|
||||
/// <returns>分页列表和总条数</returns>
|
||||
public async Task<(IEnumerable<TEntity> List, long Total)> GetPagedListAsync(
|
||||
DynamicFilterInfo dynamicFilterInfo, int page, int pageSize)
|
||||
{
|
||||
var list = await Select.WhereDynamicFilter(dynamicFilterInfo)
|
||||
.Count(out var total)
|
||||
.Page(page, pageSize)
|
||||
.ToListAsync();
|
||||
return (list, total);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user