using Tiobon.Core.Common; using Tiobon.Core.IRepository.Base; using Tiobon.Core.IServices; using Tiobon.Core.Model; using Tiobon.Core.Model.Models; using Tiobon.Core.Services.BASE; using System; using System.Threading.Tasks; using Tiobon.Core.Common.DB; using Tiobon.Core.Repository.UnitOfWorks; namespace Tiobon.Core.Services { public class GuestbookServices : BaseServices, IGuestbookServices { private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly IBaseRepository _passwordLibRepository; private readonly IPasswordLibServices _passwordLibServices; public GuestbookServices(IUnitOfWorkManage unitOfWorkManage, IBaseRepository dal, IBaseRepository passwordLibRepository, IPasswordLibServices passwordLibServices) { _unitOfWorkManage = unitOfWorkManage; _passwordLibRepository = passwordLibRepository; _passwordLibServices = passwordLibServices; } 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", TiobonId = 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 ServiceResult() { Success = true, Message = "操作完成" }; } 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 ServiceResult() { Success = false, Message = "操作异常" }; } } [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", TiobonId = 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", TiobonId = 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", TiobonId = 1, createdate = DateTime.Now, isshow = true }); await _passwordLibServices.TestTranPropagationNoTranError(); return true; } /// /// 测试嵌套事务 /// /// [UseTran(Propagation = Propagation.Required)] public async Task TestTranPropagationTran() { 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", TiobonId = 1, createdate = DateTime.Now, isshow = true }); await _passwordLibServices.TestTranPropagationTran2(); return true; } } }