Bladeren bron

支持使用Freesql工作单元开启Cap分布式事务

zhontai 1 jaar geleden
bovenliggende
commit
9762c08587

+ 73 - 0
src/platform/ZhonTai.Admin/Core/Db/UnitOfWorkExtensions.cs

@@ -0,0 +1,73 @@
+using DotNetCore.CAP;
+using DotNetCore.CAP.Transport;
+using FreeSql;
+using Microsoft.Extensions.DependencyInjection;
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace ZhonTai.Admin.Core.Db
+{
+    public class FreeSqlRepositoryPatternTransaction : CapTransactionBase
+    {
+        public FreeSqlRepositoryPatternTransaction(IDispatcher dispatcher, IUnitOfWork uow) : base(dispatcher)
+        {
+            Uow = uow;
+        }
+
+        public IUnitOfWork Uow { get; }
+
+        public override object? DbTransaction => Uow.GetOrBeginTransaction();
+
+        public override void Commit()
+        {
+            Uow.Commit();
+            Flush();
+        }
+
+        public override Task CommitAsync(CancellationToken cancellationToken = default)
+        {
+            throw new NotImplementedException();
+        }
+
+        public override void Rollback()
+        {
+            Uow.Rollback();
+        }
+
+        public override Task RollbackAsync(CancellationToken cancellationToken = default)
+        {
+            throw new NotImplementedException();
+        }
+
+        public override void Dispose()
+        {
+            Uow.Dispose();
+        }
+    }
+
+    public static class UnitOfWorkExtensions
+    {
+        /// <summary>
+        /// 开启Cap分布式事务
+        /// </summary>
+        /// <param name="unitOfWork"></param>
+        /// <param name="capPublisher"></param>
+        /// <param name="autoCommit"></param>
+        /// <remarks>
+        /// using var uow = LazyGetRequiredService&lt;UnitOfWorkManagerCloud&gt;().Begin(DbKeys.AppDb);<br/>
+        /// using var capTran = uow.BeginCapTran(LazyGetRequiredService&lt;ICapPublisher&gt;(), false);<br/>
+        /// capTran.Commit();
+        /// </remarks>
+        /// <returns></returns>
+        public static ICapTransaction BeginCapTran(this IUnitOfWork unitOfWork, ICapPublisher capPublisher, bool autoCommit = false)
+        {
+            var dispatcher = capPublisher.ServiceProvider.GetRequiredService<IDispatcher>();
+            var transaction = new FreeSqlRepositoryPatternTransaction(dispatcher, unitOfWork)
+            {
+                AutoCommit = autoCommit
+            };
+            return capPublisher.Transaction.Value = transaction;
+        }
+    }
+}

+ 14 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -1238,6 +1238,20 @@
             <typeparam name="TResult"></typeparam>
             <param name="invocation"></param>
         </member>
+        <member name="M:ZhonTai.Admin.Core.Db.UnitOfWorkExtensions.BeginTransaction(FreeSql.IUnitOfWork,DotNetCore.CAP.ICapPublisher,System.Boolean)">
+            <summary>
+            开启事务
+            </summary>
+            <param name="unitOfWork"></param>
+            <param name="capPublisher"></param>
+            <param name="autoCommit"></param>
+            <remarks>
+            using var uow = LazyGetRequiredService&lt;UnitOfWorkManagerCloud&gt;().Begin(DbKeys.AppDb);<br/>
+            using var capTran = uow.BeginTransaction(LazyGetRequiredService&lt;ICapPublisher&gt;(), false);<br/>
+            capTran.Commit();
+            </remarks>
+            <returns></returns>
+        </member>
         <member name="P:ZhonTai.Admin.Core.Dto.CreateFreeSqlTenantDto.DbKey">
             <summary>
             数据库注册键