重构服务类和仓库类

This commit is contained in:
2025-07-19 22:29:50 +08:00
parent 5cf25dd9ac
commit 7faac9aef1
25 changed files with 324 additions and 502 deletions

View File

@@ -15,26 +15,21 @@ namespace DMS.Infrastructure.Repositories;
public abstract class BaseRepository<TEntity>
where TEntity : class, new()
{
private readonly ITransaction _transaction;
private readonly SqlSugarDbContext _dbContext;
/// <summary>
/// 获取当前事务的 SqlSugarClient 实例,用于数据库操作。
/// </summary>
protected SqlSugarClient Db => _transaction.GetInstance();
protected SqlSugarClient Db => _dbContext.GetInstance();
/// <summary>
/// 获取到当前的事务
/// </summary>
/// <returns></returns>
public ITransaction GetTransaction() => _transaction;
/// <summary>
/// 初始化 BaseRepository 的新实例。
/// </summary>
/// <param name="transaction">事务管理对象,通过依赖注入提供。</param>
protected BaseRepository(ITransaction transaction)
/// <param name="dbContext">事务管理对象,通过依赖注入提供。</param>
protected BaseRepository(SqlSugarDbContext dbContext)
{
this._transaction = transaction;
this._dbContext = dbContext;
}
/// <summary>
@@ -95,6 +90,7 @@ public abstract class BaseRepository<TEntity>
NlogHelper.Info($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entities;
}
/// <summary>
/// 异步根据主键 ID 获取单个实体。
@@ -125,4 +121,36 @@ public abstract class BaseRepository<TEntity>
NlogHelper.Info($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return entity;
}
/// <summary>
/// 异步判断是否存在满足条件的实体。
/// </summary>
/// <param name="expression">查询条件的 Lambda 表达式。</param>
/// <returns>如果存在则返回 true否则返回 false。</returns>
public virtual async Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> expression)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await Db.Queryable<TEntity>().AnyAsync(expression);
stopwatch.Stop();
NlogHelper.Info($"Exists {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
public async Task BeginTranAsync()
{
await Db.BeginTranAsync();
}
public async Task CommitTranAsync()
{
await Db.CommitTranAsync();
}
public async Task RollbackTranAsync()
{
await Db.RollbackTranAsync();
}
}

View File

@@ -13,8 +13,8 @@ namespace DMS.Infrastructure.Repositories;
public class DeviceRepository : BaseRepository<DbDevice>,IDeviceRepository
{
public DeviceRepository(ITransaction transaction)
: base(transaction)
public DeviceRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}

View File

@@ -10,27 +10,19 @@ using DMS.Infrastructure.Interfaces;
namespace DMS.Infrastructure.Repositories;
public class MenuRepository : BaseRepository<DbMenu>
public class MenuRepository : BaseRepository<DbMenu>,IMenuRepository
{
public MenuRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
}
public override async Task<int> DeleteAsync(DbMenu menu)
{
return await base.DeleteAsync(menu);
}
public async Task<List<DbMenu>> GetMenuTreesAsync()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var dbMenuTree = await Db.Queryable<DbMenu>()
.ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
stopwatch.Stop();
NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms");
return dbMenuTree;
@@ -54,9 +46,4 @@ public class MenuRepository : BaseRepository<DbMenu>
return result;
}
public async Task<DbMenu> GetMainMenuByNameAsync(string name)
{
var dbMenu= await Db.Queryable<DbMenu>().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu);
return dbMenu;
}
}

View File

@@ -49,9 +49,9 @@ public class VarDataRepository : BaseRepository<DbVariable>
// public VarDataRepository(IMapper mapper)
// public VarDataRepository(IMapper _mapper)
// {
// _mapper = mapper;
// _mapper = _mapper;
// }
/*