z8018
2026-02-11 b8fb68b44c29e4667f6ea5746119413809a60a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 
using Microsoft.Extensions.Logging;
using KH.WMS.Core.Database.Repositories;
using KH.WMS.Core.DependencyInjection.ServiceLifetimes;
 
namespace KH.WMS.Core.Database.UnitOfWorks;
 
/// <summary>
/// 工作单元实现(面向接口,支持事务嵌套)
/// </summary>
[RegisteredService(Lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped)]
public class UnitOfWork : IUnitOfWork
{
    private readonly IDbContext _dbContext;
    private readonly ILogger<UnitOfWork> _logger;
    private bool _disposed;
 
    public UnitOfWork(IDbContext dbContext, ILogger<UnitOfWork> logger)
    {
        _dbContext = dbContext;
        _logger = logger;
    }
 
    /// <summary>
    /// 获取数据库上下文
    /// </summary>
    public IDbContext DbContext => _dbContext;
 
    /// <summary>
    /// 开始事务
    /// </summary>
    public async Task BeginTransactionAsync(System.Data.IsolationLevel isolationLevel = System.Data.IsolationLevel.ReadCommitted)
    {
        await _dbContext.BeginTransactionAsync(isolationLevel);
        _logger.LogDebug("工作单元事务已开启");
    }
 
    /// <summary>
    /// 提交事务
    /// </summary>
    public async Task CommitAsync()
    {
        await _dbContext.CommitTransactionAsync();
        _logger.LogDebug("工作单元事务已提交");
    }
 
    /// <summary>
    /// 回滚事务
    /// </summary>
    public async Task RollbackAsync()
    {
        await _dbContext.RollbackTransactionAsync();
        _logger.LogDebug("工作单元事务已回滚");
    }
 
    /// <summary>
    /// 获取仓储
    /// </summary>
    public IRepository<T, TKey> GetRepository<T, TKey>() where T : class where TKey : struct
    {
        return _dbContext.GetRepository<T, TKey>();
    }
 
    /// <summary>
    /// 执行 SQL 命令
    /// </summary>
    public async Task<int> ExecuteSqlAsync<TKey>(string sql, params object?[] parameters) where TKey : struct
    {
        var repository = GetRepository<dynamic, TKey>();
        // 使用 Ado 执行命令
        // 这里需要根据实际的 ISqlSugarClient 实现来执行 SQL
        return await Task.FromResult(0);
    }
 
    /// <summary>
    /// 执行 SQL 命令(返回列表)
    /// </summary>
    public async Task<List<T>> ExecuteSqlAsync<T, TKey>(string sql, params object?[] parameters) where T : class where TKey : struct
    {
        var repository = GetRepository<T, TKey>();
        // 使用 Ado 执行查询
        // 这里需要根据实际的 ISqlSugarClient 实现来执行 SQL
        return await Task.FromResult(new List<T>());
    }
 
    public void Dispose()
    {
        if (!_disposed)
        {
            _dbContext?.Dispose();
            _disposed = true;
        }
    }
 
    IRepository<T, TKey> IUnitOfWork.GetRepository<T, TKey>() where T : class where TKey : struct
    {
        return _dbContext.GetRepository<T, TKey>();
    }
}