using Opsoft.NetCore.O3App.Domain; using Opsoft.NetCore.O3App.Shared; using System; using System.Threading; namespace Opsoft.NetCore.O3App.Infrastructure.Repositories { internal class DapperUnitOfWork : IDapperUnitOfWork { private readonly IDapperContext dapperContext; public DapperUnitOfWork(IDapperContext dapperContext) { this.dapperContext = Guard.ArgumentNotNull(dapperContext, nameof(dapperContext)); this.dapperContext.BeginTransaction(); } public void Dispose() { if (dapperContext.IsTransactionStarted) { dapperContext.Rollback(); } DebugPrint($"{nameof(DapperUnitOfWork)} disposed."); } public void SaveChanges() { if (!dapperContext.IsTransactionStarted) { throw new InvalidOperationException("Transaction have already been commited or disposed."); } dapperContext.Commit(); } protected virtual void DebugPrint(string message) { #if DEBUG Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} [INFO] Thread {1}: {2}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, message); #endif } } }