Files
DMS/DMS.Infrastructure/Repositories/RepositoryManager.cs

105 lines
3.6 KiB
C#
Raw Normal View History

2025-07-21 22:02:42 +08:00
using AutoMapper;
2025-07-21 18:49:49 +08:00
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
using DMS.Infrastructure.Data;
using SqlSugar;
namespace DMS.Infrastructure.Repositories;
2025-07-24 19:58:34 +08:00
/// <summary>
/// 仓储管理器实现类,负责管理所有具体的仓储实例,并提供事务管理功能。
/// 实现 <see cref="IRepositoryManager"/> 接口。
/// </summary>
2025-07-21 18:49:49 +08:00
public class RepositoryManager : IRepositoryManager
{
private readonly SqlSugarClient _db;
2025-07-21 22:02:42 +08:00
private readonly IMapper _mapper;
2025-07-21 18:49:49 +08:00
private readonly SqlSugarDbContext _dbContext;
2025-07-24 19:58:34 +08:00
/// <summary>
/// 构造函数,注入 AutoMapper 和 SqlSugarDbContext。
/// 在此初始化所有具体的仓储实例。
/// </summary>
/// <param name="mapper">AutoMapper 实例,用于实体模型和数据库模型之间的映射。</param>
/// <param name="dbContext">SqlSugar 数据库上下文,用于数据库操作。</param>
2025-07-21 22:02:42 +08:00
public RepositoryManager(IMapper mapper, SqlSugarDbContext dbContext)
2025-07-21 18:49:49 +08:00
{
2025-07-21 22:02:42 +08:00
_mapper = mapper;
2025-07-21 18:49:49 +08:00
_dbContext = dbContext;
_db = dbContext.GetInstance();
2025-07-24 19:58:34 +08:00
// 初始化各个仓储实例
InitializeRepository=new InitializeRepository(dbContext);
2025-07-21 22:02:42 +08:00
Devices = new DeviceRepository(mapper, dbContext);
VariableTables = new VariableTableRepository(mapper, dbContext);
Variables = new VariableRepository(mapper, dbContext);
MqttServers = new MqttServerRepository(mapper, dbContext);
VariableMqttAliases = new VariableMqttAliasRepository(mapper, dbContext);
Menus = new MenuRepository(mapper, dbContext);
VariableHistories = new VariableHistoryRepository(mapper, dbContext);
Users = new UserRepository(mapper, dbContext);
2025-07-21 18:49:49 +08:00
}
2025-07-24 19:58:34 +08:00
/// <summary>
/// 释放数据库连接资源。
/// </summary>
2025-07-21 18:49:49 +08:00
public void Dispose()
{
_db.Close();
}
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取设备仓储实例。
/// </summary>
2025-07-21 18:49:49 +08:00
public IDeviceRepository Devices { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取变量表仓储实例。
/// </summary>
2025-07-21 18:49:49 +08:00
public IVariableTableRepository VariableTables { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取变量仓储实例。
/// </summary>
2025-07-21 18:49:49 +08:00
public IVariableRepository Variables { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取MQTT服务器仓储实例。
/// </summary>
2025-07-21 18:49:49 +08:00
public IMqttServerRepository MqttServers { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取变量MQTT别名仓储实例。
/// </summary>
2025-07-21 18:49:49 +08:00
public IVariableMqttAliasRepository VariableMqttAliases { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取菜单仓储实例。
/// </summary>
2025-07-21 18:49:49 +08:00
public IMenuRepository Menus { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取变量历史仓储实例。
/// </summary>
2025-07-21 18:49:49 +08:00
public IVariableHistoryRepository VariableHistories { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取用户仓储实例。
/// </summary>
2025-07-21 18:49:49 +08:00
public IUserRepository Users { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 获取初始化仓储实例。
/// </summary>
public IInitializeRepository InitializeRepository { get; set; }
2025-07-24 19:58:34 +08:00
/// <summary>
/// 异步开始数据库事务。
/// </summary>
/// <returns>表示异步操作的任务。</returns>
2025-07-21 18:49:49 +08:00
public async Task BeginTranAsync() => await _db.BeginTranAsync();
2025-07-24 19:58:34 +08:00
/// <summary>
/// 异步提交数据库事务。
/// </summary>
/// <returns>表示异步操作的任务。</returns>
2025-07-21 18:49:49 +08:00
public async Task CommitAsync() => await _db.CommitTranAsync();
2025-07-24 19:58:34 +08:00
/// <summary>
/// 异步回滚数据库事务。
/// </summary>
/// <returns>表示异步操作的任务。</returns>
2025-07-21 18:49:49 +08:00
public async Task RollbackAsync() => await _db.RollbackTranAsync();
}