Files
GX.O3.WMS/APP.O3/Infrastructure/SeedWork/DapperContext.cs
T

113 lines
4.0 KiB
C#
Executable File

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<int> ExecuteAsync(string sql, object param = null, int? commandTimeout = null)
{
return await SqlMapper.ExecuteAsync(Connection, sql, param, transaction, commandTimeout, CommandType.Text);
}
public async Task<IDataReader> ExecuteReaderAsync(string sql, object param = null, int? commandTimeout = null)
{
return await SqlMapper.ExecuteReaderAsync(Connection, sql, param, transaction, commandTimeout, CommandType.Text);
}
public async Task<T> ExecuteScalarAsync<T>(string sql, object param = null, int? commandTimeout = null)
{
return await SqlMapper.ExecuteScalarAsync<T>(Connection, sql, param, transaction, commandTimeout, CommandType.Text);
}
public async Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null, int? commandTimeout = null)
{
return await SqlMapper.QueryAsync<T>(Connection, sql, param, transaction, commandTimeout, CommandType.Text);
}
public async Task<long> AddAsync<T>(T entity, int? commandTimeout = null) where T : class
{
return await SqlMapperExtensions.InsertAsync(Connection, entity, transaction, commandTimeout);
}
public async Task<bool> UpdateAsync<T>(T entity, int? commandTimeout = null) where T : class
{
return await SqlMapperExtensions.UpdateAsync(Connection, entity, transaction, commandTimeout);
}
public async Task<bool> DeleteAsync<T>(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.");
}
}
}