using DouyinApi.Common; using DouyinApi.IRepository.Base; using DouyinApi.IServices; using DouyinApi.Model; using DouyinApi.Model.Models; using DouyinApi.Services.BASE; using System; using System.Threading.Tasks; using DouyinApi.Common.DB; using DouyinApi.Common.Utility; using DouyinApi.Repository.UnitOfWorks; using SqlSugar; namespace DouyinApi.Services { public class GuestbookServices : BaseServices, IGuestbookServices { private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly IBaseRepository _passwordLibRepository; private readonly IPasswordLibServices _passwordLibServices; private readonly ISqlSugarClient _db; private SqlSugarScope db => _db as SqlSugarScope; public GuestbookServices(IUnitOfWorkManage unitOfWorkManage, IBaseRepository dal, IBaseRepository passwordLibRepository, IPasswordLibServices passwordLibServices, ISqlSugarClient db) { _unitOfWorkManage = unitOfWorkManage; _passwordLibRepository = passwordLibRepository; _passwordLibServices = passwordLibServices; _db = db; } public async Task> TestTranInRepository() { try { Console.WriteLine($""); Console.WriteLine($"事务操作开始"); using (var uow = _unitOfWorkManage.CreateUnitOfWork()) { Console.WriteLine($""); Console.WriteLine($"insert a data into the table PasswordLib now."); var insertPassword = await _passwordLibRepository.Add(new PasswordLib() { IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now }); var passwords = await _passwordLibRepository.Query(d => d.IsDeleted == false); Console.WriteLine($"second time : the count of passwords is :{passwords.Count}"); //...... Console.WriteLine($""); var guestbooks = await BaseDal.Query(); Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}"); int ex = 0; Console.WriteLine($"\nThere's an exception!!"); int throwEx = 1 / ex; Console.WriteLine($"insert a data into the table Guestbook now."); var insertGuestbook = await BaseDal.Add(new Guestbook() { username = "bbb", blogId = 1, createdate = DateTime.Now, isshow = true }); guestbooks = await BaseDal.Query(); Console.WriteLine($"second time : the count of guestbooks is :{guestbooks.Count}"); uow.Commit(); } return new MessageModel() { success = true, msg = "操作完成" }; } catch (Exception) { var passwords = await _passwordLibRepository.Query(); Console.WriteLine($"third time : the count of passwords is :{passwords.Count}"); var guestbooks = await BaseDal.Query(); Console.WriteLine($"third time : the count of guestbooks is :{guestbooks.Count}"); return new MessageModel() { success = false, msg = "操作异常" }; } } [UseTran] public async Task TestTranInRepositoryAOP() { var passwords = await _passwordLibRepository.Query(); Console.WriteLine($"first time : the count of passwords is :{passwords.Count}"); Console.WriteLine($"insert a data into the table PasswordLib now."); var insertPassword = await _passwordLibRepository.Add(new PasswordLib() { IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now }); passwords = await _passwordLibRepository.Query(d => d.IsDeleted == false); Console.WriteLine($"second time : the count of passwords is :{passwords.Count}"); //...... Console.WriteLine($""); var guestbooks = await BaseDal.Query(); Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}"); int ex = 0; Console.WriteLine($"\nThere's an exception!!"); int throwEx = 1 / ex; Console.WriteLine($"insert a data into the table Guestbook now."); var insertGuestbook = await BaseDal.Add(new Guestbook() { username = "bbb", blogId = 1, createdate = DateTime.Now, isshow = true }); guestbooks = await BaseDal.Query(); Console.WriteLine($"second time : the count of guestbooks is :{guestbooks.Count}"); return true; } /// /// 测试使用同事务 /// /// [UseTran(Propagation = Propagation.Required)] public async Task TestTranPropagation() { var guestbooks = await base.Query(); Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}"); var insertGuestbook = await base.Add(new Guestbook() { username = "bbb", blogId = 1, createdate = DateTime.Now, isshow = true }); await _passwordLibServices.TestTranPropagation2(); return true; } /// /// 测试无事务 Mandatory传播机制报错 /// /// public async Task TestTranPropagationNoTran() { var guestbooks = await base.Query(); Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}"); var insertGuestbook = await base.Add(new Guestbook() { username = "bbb", blogId = 1, createdate = DateTime.Now, isshow = true }); await _passwordLibServices.TestTranPropagationNoTranError(); return true; } /// /// 测试嵌套事务 /// /// [UseTran(Propagation = Propagation.Required)] public async Task TestTranPropagationTran() { var guestbooks = await base.Query(); guestbooks = await base.Query(); Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}"); Console.WriteLine(base.Db.ContextID); var insertGuestbook = await base.Add(new Guestbook() { username = "bbb", blogId = 1, createdate = DateTime.Now, isshow = true }); await _passwordLibServices.TestTranPropagationTran2(); return true; } [UseTran(Propagation = Propagation.Required)] public async Task TestTranPropagationTran2() { await Db.Insertable(new Guestbook() { username = "bbb", blogId = 1, createdate = DateTime.Now, isshow = true }).ExecuteReturnSnowflakeIdAsync(); await Db.Insertable(new PasswordLib() { PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now }).ExecuteReturnSnowflakeIdAsync(); await _passwordLibServices.TestTranPropagationTran2(); Console.WriteLine("完成"); } public async Task TestTranPropagationTran3() { try { Console.WriteLine("Begin Transaction Before:" + db.ContextID); db.BeginTran(); Console.WriteLine("Begin Transaction After:" + db.ContextID); await db.Insertable(new Guestbook() { username = "bbb", blogId = 1, createdate = DateTime.Now, isshow = true }).ExecuteReturnSnowflakeIdAsync(); await db.Insertable(new PasswordLib() { PLID = IdGeneratorUtility.NextId(), IsDeleted = false, plAccountName = "aaa", plCreateTime = DateTime.Now }).ExecuteReturnSnowflakeIdAsync(); await _passwordLibServices.TestTranPropagationTran3(); db.CommitTran(); Console.WriteLine("完成"); } catch (Exception e) { db.RollbackTran(); throw; } } } }