using Dapper; using Dapper.Contrib.Extensions; using Opsoft.NetCore.O3App.Domain; using Opsoft.NetCore.O3App.Shared; using System; using System.Collections.Generic; using System.Data; using System.Threading; using System.Threading.Tasks; namespace Opsoft.NetCore.O3App.Infrastructure { internal class DapperContext : IDapperContext { private IDbTransaction transaction; public bool IsTransactionStarted { get; private set; } public IDbConnection Connection { get; private set; } public DapperContext(IDbConnection connection) { Connection = Guard.ArgumentNotNull(connection, nameof(connection)); if (Connection.State == ConnectionState.Closed) { Connection.Open(); DebugPrint("Connection started."); } } 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 } public void BeginTransaction() { if (IsTransactionStarted) throw new InvalidOperationException("Transaction is already started."); transaction = Connection.BeginTransaction(); IsTransactionStarted = true; DebugPrint("Transaction started."); } public void Commit() { if (!IsTransactionStarted) throw new InvalidOperationException("No transaction started."); transaction.Commit(); IsTransactionStarted = false; DebugPrint("Transaction committed."); } public void Rollback() { if (!IsTransactionStarted) throw new InvalidOperationException("No transaction started."); transaction.Rollback(); IsTransactionStarted = false; DebugPrint("Transaction rollbacked and disposed."); } public async Task ExecuteAsync(string sql, object param = null, int? commandTimeout = null) { return await SqlMapper.ExecuteAsync(Connection, sql, param, transaction, commandTimeout, CommandType.Text); } public async Task ExecuteReaderAsync(string sql, object param = null, int? commandTimeout = null) { return await SqlMapper.ExecuteReaderAsync(Connection, sql, param, transaction, commandTimeout, CommandType.Text); } public async Task ExecuteScalarAsync(string sql, object param = null, int? commandTimeout = null) { return await SqlMapper.ExecuteScalarAsync(Connection, sql, param, transaction, commandTimeout, CommandType.Text); } public async Task> QueryAsync(string sql, object param = null, int? commandTimeout = null) { return await SqlMapper.QueryAsync(Connection, sql, param, transaction, commandTimeout, CommandType.Text); } public async Task AddAsync(T entity, int? commandTimeout = null) where T : class { return await SqlMapperExtensions.InsertAsync(Connection, entity, transaction, commandTimeout); } public async Task UpdateAsync(T entity, int? commandTimeout = null) where T : class { return await SqlMapperExtensions.UpdateAsync(Connection, entity, transaction, commandTimeout); } public async Task DeleteAsync(T entity, int? commandTimeout = null) where T : class { return await SqlMapperExtensions.DeleteAsync(Connection, entity, transaction, commandTimeout); } public void Dispose() { if (IsTransactionStarted) { Rollback(); } if (transaction != null) { transaction.Dispose(); } Connection.Close(); DebugPrint("Connection closed."); } } }