using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using DapperExtensions.Mapper; using DapperExtensions.Sql; using System.Data.Common; namespace DapperExtensions { public interface IDatabase : IDisposable { bool HasActiveTransaction { get; } DbConnection Connection { get; } void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted); void Commit(); void Rollback(); void RunInTransaction(Action action); T RunInTransaction(Func func); T Get(dynamic id, DbTransaction transaction, int? commandTimeout = null) where T : class; T Get(dynamic id, int? commandTimeout = null) where T : class; void Insert(IEnumerable entities, DbTransaction transaction, int? commandTimeout = null) where T : class; void Insert(IEnumerable entities, int? commandTimeout = null) where T : class; dynamic Insert(T entity, DbTransaction transaction, int? commandTimeout = null) where T : class; dynamic Insert(T entity, int? commandTimeout = null) where T : class; bool Update(T entity, DbTransaction transaction, int? commandTimeout = null) where T : class; bool Update(T entity, int? commandTimeout = null) where T : class; bool Delete(T entity, DbTransaction transaction, int? commandTimeout = null) where T : class; bool Delete(T entity, int? commandTimeout = null) where T : class; bool Delete(object predicate, DbTransaction transaction, int? commandTimeout = null) where T : class; bool Delete(object predicate, int? commandTimeout = null) where T : class; IEnumerable GetList(object predicate, IList sort, DbTransaction transaction, int? commandTimeout = null, bool buffered = true) where T : class; IEnumerable GetList(object predicate = null, IList sort = null, int? commandTimeout = null, bool buffered = true) where T : class; IEnumerable GetPage(object predicate, IList sort, int page, int resultsPerPage, DbTransaction transaction, int? commandTimeout = null, bool buffered = true) where T : class; IEnumerable GetPage(object predicate, IList sort, int page, int resultsPerPage, int? commandTimeout = null, bool buffered = true) where T : class; IEnumerable GetSet(object predicate, IList sort, int firstResult, int maxResults, DbTransaction transaction, int? commandTimeout, bool buffered) where T : class; IEnumerable GetSet(object predicate, IList sort, int firstResult, int maxResults, int? commandTimeout, bool buffered) where T : class; int Count(object predicate, DbTransaction transaction, int? commandTimeout = null) where T : class; int Count(object predicate, int? commandTimeout = null) where T : class; IMultipleResultReader GetMultiple(GetMultiplePredicate predicate, DbTransaction transaction, int? commandTimeout = null); IMultipleResultReader GetMultiple(GetMultiplePredicate predicate, int? commandTimeout = null); void ClearCache(); Guid GetNextGuid(); IClassMapper GetMap() where T : class; } public class Database : IDatabase { private readonly IDapperImplementor _dapper; private DbTransaction _transaction; public Database(DbConnection connection, ISqlGenerator sqlGenerator) { _dapper = new DapperImplementor(sqlGenerator); Connection = connection; if (Connection.State != ConnectionState.Open) { Connection.Open(); } } public bool HasActiveTransaction { get { return _transaction != null; } } public DbConnection Connection { get; private set; } public void Dispose() { if (Connection.State != ConnectionState.Closed) { if (_transaction != null) { _transaction.Rollback(); } Connection.Close(); } } public void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) { _transaction = Connection.BeginTransaction(isolationLevel); } public void Commit() { _transaction.Commit(); _transaction = null; } public void Rollback() { _transaction.Rollback(); _transaction = null; } public void RunInTransaction(Action action) { BeginTransaction(); try { action(); Commit(); } catch (Exception ex) { if (HasActiveTransaction) { Rollback(); } throw ex; } } public T RunInTransaction(Func func) { BeginTransaction(); try { T result = func(); Commit(); return result; } catch (Exception ex) { if (HasActiveTransaction) { Rollback(); } throw ex; } } public T Get(dynamic id, DbTransaction transaction, int? commandTimeout) where T : class { return (T)_dapper.Get(Connection, id, transaction, commandTimeout); } public T Get(dynamic id, int? commandTimeout) where T : class { return (T)_dapper.Get(Connection, id, _transaction, commandTimeout); } public void Insert(IEnumerable entities, DbTransaction transaction, int? commandTimeout) where T : class { _dapper.Insert(Connection, entities, transaction, commandTimeout); } public void Insert(IEnumerable entities, int? commandTimeout) where T : class { _dapper.Insert(Connection, entities, _transaction, commandTimeout); } public dynamic Insert(T entity, DbTransaction transaction, int? commandTimeout) where T : class { return _dapper.Insert(Connection, entity, transaction, commandTimeout); } public dynamic Insert(T entity, int? commandTimeout) where T : class { return _dapper.Insert(Connection, entity, _transaction, commandTimeout); } public bool Update(T entity, DbTransaction transaction, int? commandTimeout) where T : class { return _dapper.Update(Connection, entity, transaction, commandTimeout); } public bool Update(T entity, int? commandTimeout) where T : class { return _dapper.Update(Connection, entity, _transaction, commandTimeout); } public bool Delete(T entity, DbTransaction transaction, int? commandTimeout) where T : class { return _dapper.Delete(Connection, entity, transaction, commandTimeout); } public bool Delete(T entity, int? commandTimeout) where T : class { return _dapper.Delete(Connection, entity, _transaction, commandTimeout); } public bool Delete(object predicate, DbTransaction transaction, int? commandTimeout) where T : class { return _dapper.Delete(Connection, predicate, transaction, commandTimeout); } public bool Delete(object predicate, int? commandTimeout) where T : class { return _dapper.Delete(Connection, predicate, _transaction, commandTimeout); } public IEnumerable GetList(object predicate, IList sort, DbTransaction transaction, int? commandTimeout, bool buffered) where T : class { return _dapper.GetList(Connection, predicate, sort, transaction, commandTimeout, buffered); } public IEnumerable GetList(object predicate, IList sort, int? commandTimeout, bool buffered) where T : class { return _dapper.GetList(Connection, predicate, sort, _transaction, commandTimeout, buffered); } public IEnumerable GetPage(object predicate, IList sort, int page, int resultsPerPage, DbTransaction transaction, int? commandTimeout, bool buffered) where T : class { return _dapper.GetPage(Connection, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered); } public IEnumerable GetPage(object predicate, IList sort, int page, int resultsPerPage, int? commandTimeout, bool buffered) where T : class { return _dapper.GetPage(Connection, predicate, sort, page, resultsPerPage, _transaction, commandTimeout, buffered); } public IEnumerable GetSet(object predicate, IList sort, int firstResult, int maxResults, DbTransaction transaction, int? commandTimeout, bool buffered) where T : class { return _dapper.GetSet(Connection, predicate, sort, firstResult, maxResults, transaction, commandTimeout, buffered); } public IEnumerable GetSet(object predicate, IList sort, int firstResult, int maxResults, int? commandTimeout, bool buffered) where T : class { return _dapper.GetSet(Connection, predicate, sort, firstResult, maxResults, _transaction, commandTimeout, buffered); } public int Count(object predicate, DbTransaction transaction, int? commandTimeout) where T : class { return _dapper.Count(Connection, predicate, transaction, commandTimeout); } public int Count(object predicate, int? commandTimeout) where T : class { return _dapper.Count(Connection, predicate, _transaction, commandTimeout); } public IMultipleResultReader GetMultiple(GetMultiplePredicate predicate, DbTransaction transaction, int? commandTimeout) { return _dapper.GetMultiple(Connection, predicate, transaction, commandTimeout); } public IMultipleResultReader GetMultiple(GetMultiplePredicate predicate, int? commandTimeout) { return _dapper.GetMultiple(Connection, predicate, _transaction, commandTimeout); } public void ClearCache() { _dapper.SqlGenerator.Configuration.ClearCache(); } public Guid GetNextGuid() { return _dapper.SqlGenerator.Configuration.GetNextGuid(); } public IClassMapper GetMap() where T : class { return _dapper.SqlGenerator.Configuration.GetMap(); } } }