diff --git a/Dapper.Data NET40/Dapper.Data NET40.csproj b/Dapper.Data NET40/Dapper.Data NET40.csproj
index 1238831..80a3c7e 100644
--- a/Dapper.Data NET40/Dapper.Data NET40.csproj
+++ b/Dapper.Data NET40/Dapper.Data NET40.csproj
@@ -52,9 +52,9 @@
false
-
+
False
- packages\Dapper.1.27\lib\net40\Dapper.dll
+ ..\Dapper.Data NET45\packages\Dapper.1.29\lib\net40\Dapper.dll
@@ -66,13 +66,16 @@
-
+
+
+
+
diff --git a/Dapper.Data NET40/Data/CommandDefinitionHelper.cs b/Dapper.Data NET40/Data/CommandDefinitionHelper.cs
deleted file mode 100644
index 8277667..0000000
--- a/Dapper.Data NET40/Data/CommandDefinitionHelper.cs
+++ /dev/null
@@ -1,22 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Dapper.Data
-{
- //public static class CommandDefinitionHelper
- //{
- // public static CommandDefinition CommandDefinition(this string query)
- // {
- // return new CommandDefinition(query);
- // }
-
- // public static CommandDefinition AddParams(CommandDefinition commandDefinition, object parameters)
- // {
- // commandDefinition.Parameters = parameters;
- // return commandDefinition;
- // }
- //}
-}
diff --git a/Dapper.Data NET40/Data/DbConnectionFactory.cs b/Dapper.Data NET40/Data/DbConnectionFactory.cs
index 5157288..87dc5b8 100644
--- a/Dapper.Data NET40/Data/DbConnectionFactory.cs
+++ b/Dapper.Data NET40/Data/DbConnectionFactory.cs
@@ -62,8 +62,7 @@ internal static IDbConnection CreateConnection(this DbProviderFactory sender, st
///
internal static IDbConnection CreateOpenedConnection(this DbProviderFactory sender, string connectionString)
{
- var connection = sender.CreateConnection();
- connection.ConnectionString = connectionString;
+ var connection = sender.CreateConnection(connectionString);
connection.Open();
return connection;
}
diff --git a/Dapper.Data NET40/Data/DbContext.cs b/Dapper.Data NET40/Data/DbContext.cs
index 81a61b1..596f260 100644
--- a/Dapper.Data NET40/Data/DbContext.cs
+++ b/Dapper.Data NET40/Data/DbContext.cs
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
+using System.ComponentModel;
+using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
+using Dapper.Data.Generic;
namespace Dapper.Data
{
@@ -38,19 +41,11 @@ IEnumerable Query(
int? commandTimeout = 0
);
}
- ///
- /// Default behavior exposed by DbContext helps with injection
- ///
- public interface IDbContext : IDbCommand
- {
- void Batch(Action action);
- TResult Batch(Func func);
- }
///
/// Interface to help with transaction managment
///
- public interface ISession : IDbCommand
+ public interface ISession : IDbCommand, IDisposable
{
void BeginTransaction();
void BeginTransaction(IsolationLevel il);
@@ -73,192 +68,175 @@ public interface ISession : IDbCommand
}
///
- /// Light weight DbContext implementation based on dapper
- /// Use it to create your own DbContext
- /// It will help manage connection life time and transactions
+ /// Default behavior exposed by DbContext helps with injection
///
- public abstract class DbContext : IDbContext
+ public interface IDbContext : IDbContext
+ { }
+
+
+ ///
+ /// Default Session implementation
+ ///
+ class Session : ISession
{
- protected DbContext(string connectionName)
- : this(new DbConnectionFactory(connectionName))
- {}
+ IDbConnection _connection;
+ IDbTransaction _transaction;
- protected DbContext(IDbConnectionFactory connectionFactory)
+ public Session(IDbConnectionFactory connectionFactory)
+ : this(connectionFactory.CreateAndOpen())
+ { }
+
+ private Session(IDbConnection connection)
{
- ConnectionFactory = connectionFactory;
+ _connection = connection;
+ _transaction = null;
}
- public virtual IDbConnectionFactory ConnectionFactory
+ public void BeginTransaction()
{
- get;
- private set;
+ if (_transaction == null)
+ { _transaction = _connection.BeginTransaction(); }
}
- ///
- /// Enables execution of multiple statements and helps with
- /// transaction management
- ///
- public virtual void Batch(Action action)
+ public void BeginTransaction(IsolationLevel il)
{
- using (var con = ConnectionFactory.CreateAndOpen())
- {
- try
- {
- action(new Session(con));
- }
- finally
- {
- con.Close();
- }
- }
+ if (_transaction == null)
+ { _transaction = _connection.BeginTransaction(il); }
}
- ///
- /// Enables execution of multiple statements and helps with
- /// transaction management
- ///
- public virtual TResult Batch(Func func)
+ public void CommitTransaction()
{
- using (var con = ConnectionFactory.CreateAndOpen())
+ if (_transaction != null)
{
- try
- {
- return func(new Session(con));
- }
- finally
- {
- con.Close();
- }
+ _transaction.Commit();
}
+ _transaction = null;
}
- class Session : ISession
+ public void RollbackTransaction()
{
- readonly IDbConnection _connection;
- IDbTransaction _transaction;
-
- public Session(IDbConnection connection)
+ if (_transaction != null)
{
- _connection = connection;
- _transaction = null;
+ _transaction.Rollback();
}
+ _transaction = null;
+ }
- public void BeginTransaction()
- {
- if (_transaction == null)
- { _transaction = _connection.BeginTransaction(); }
- }
+ public IDbConnection Connection { get { return _connection; } }
- public void BeginTransaction(IsolationLevel il)
- {
- if (_transaction == null)
- { _transaction = _connection.BeginTransaction(il); }
- }
+ public int Execute(CommandDefinition command)
+ {
+ return _connection.Execute(command);
+ }
- public void CommitTransaction()
- {
- if (_transaction != null)
- {
- _transaction.Commit();
- }
- _transaction = null;
- }
+ public IEnumerable Query(CommandDefinition command)
+ {
+ return _connection.Query(command);
+ }
- public void RollbackTransaction()
- {
- if (_transaction != null)
- {
- _transaction.Rollback();
- }
- _transaction = null;
- }
+ public IEnumerable Query(string sql, Func map, object param = null, bool buffered = true, string splitOn = "Id", CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return _connection.Query(sql, map, param, _transaction, buffered, splitOn, commandTimeout, commandType);
+ }
- public IDbConnection Connection { get { return _connection; } }
+ public IEnumerable Query(string sql, Func map, object param = null, bool buffered = true, string splitOn = "Id", CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return _connection.Query(sql, map, param, _transaction, buffered, splitOn, commandTimeout, commandType);
+ }
- public int Execute(CommandDefinition command)
- {
- return _connection.Execute(command);
- }
+ public IEnumerable Query(string sql, Func map, object param = null, bool buffered = true, string splitOn = "Id", CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return _connection.Query(sql, map, param, _transaction, buffered, splitOn, commandTimeout, commandType);
+ }
- public IEnumerable Query(Dapper.CommandDefinition command)
- {
- return _connection.Query(command);
- }
+ public IEnumerable Query(string sql, Func map, object param = null, bool buffered = true, string splitOn = "Id", CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return _connection.Query(sql, map, param, _transaction, buffered, splitOn, commandTimeout, commandType);
+ }
- public IEnumerable Query(string sql, Func map, object param = null, bool buffered = true, string splitOn = "Id", CommandType? commandType = null, int? commandTimeout = 0)
- {
- return _connection.Query(sql, map, param, _transaction, buffered, splitOn, commandTimeout, commandType);
- }
+ public int Execute(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return _connection.Execute(sql, param, _transaction, commandTimeout, commandType);
+ }
- public IEnumerable Query(string sql, Func map, object param = null, bool buffered = true, string splitOn = "Id", CommandType? commandType = null, int? commandTimeout = 0)
- {
- return _connection.Query(sql, map, param, _transaction, buffered, splitOn, commandTimeout, commandType);
- }
+ public IEnumerable Query(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return typeof(T) == typeof(IDictionary) ? _connection.Query(sql, param, _transaction, true, commandTimeout, commandType).OfType() : _connection.Query(sql, param, _transaction, true, commandTimeout, commandType);
+ }
- public IEnumerable Query(string sql, Func map, object param = null, bool buffered = true, string splitOn = "Id", CommandType? commandType = null, int? commandTimeout = 0)
- {
- return _connection.Query(sql, map, param, _transaction, buffered, splitOn, commandTimeout, commandType);
- }
+ public IEnumerable Query(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return _connection.Query(sql, param, null, true, commandTimeout, commandType);
+ }
- public IEnumerable Query(string sql, Func map, object param = null, bool buffered = true, string splitOn = "Id", CommandType? commandType = null, int? commandTimeout = 0)
- {
- return _connection.Query(sql, map, param, _transaction, buffered, splitOn, commandTimeout, commandType);
- }
+ public IEnumerable Query(Type type, string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return _connection.Query(type, sql, param, _transaction, true, commandTimeout, commandType);
+ }
- public int Execute(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
- {
- return _connection.Execute(sql, param, _transaction, commandTimeout, commandType);
- }
+ public SqlMapper.GridReader QueryMultiple(CommandDefinition command)
+ {
+ return _connection.QueryMultiple(command);
+ }
- public IEnumerable Query(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
- {
- if (typeof(T) == typeof(IDictionary))
- {
- return _connection.Query(sql, param, _transaction, true, commandTimeout, commandType).OfType();
- }
- return _connection.Query(sql, param, _transaction, true, commandTimeout, commandType);
- }
+ public SqlMapper.GridReader QueryMultiple(string sql, dynamic param = null, CommandType? commandType = null, int? commandTimeout = 0)
+ {
+ return _connection.QueryMultiple(new CommandDefinition(sql, param, _transaction, commandTimeout, commandType));
+ }
- public IEnumerable Query(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
- {
- return _connection.Query(sql, param, null, true, commandTimeout, commandType);
- }
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ void IDisposable.Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
- public IEnumerable Query(Type type, string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!disposing)
+ return;
+ try
{
- return _connection.Query(type, sql, param, _transaction, true, commandTimeout, commandType);
+ if (_transaction == null)
+ { return; }
+ _transaction.Rollback();
+ _transaction.Dispose();
+ _transaction = null;
}
-
- public Dapper.SqlMapper.GridReader QueryMultiple(Dapper.CommandDefinition command)
+ catch (Exception ex)
{
- return _connection.QueryMultiple(command);
+ Trace.TraceError(ex.ToString());
}
-
- public Dapper.SqlMapper.GridReader QueryMultiple(string sql, dynamic param = null, System.Data.CommandType? commandType = null, int? commandTimeout = 0)
+ finally
{
- return _connection.QueryMultiple(new CommandDefinition(sql, param, _transaction, commandTimeout, commandType));
+ if (_connection != null)
+ {
+ _connection.Close();
+ _connection.Dispose();
+ _connection = null;
+ }
}
-
- }
-
- public int Execute(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
- {
- return Batch(s => s.Execute(sql, param, commandType, commandTimeout));
}
+ }
- public IEnumerable Query(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
- {
- return Batch(s => s.Query(sql, param, commandType, commandTimeout));
- }
+ ///
+ /// Light weight DbContext implementation based on dapper
+ /// Use it to create your own DbContext
+ /// It will help manage connection life time and transactions
+ ///
+ public abstract class DbContext : DbContext, IDbContext
+ {
+ protected DbContext(string connectionName)
+ : base(connectionName)
+ { }
- public IEnumerable Query(string sql, object param = null, CommandType? commandType = null, int? commandTimeout = 0)
- {
- return Batch(s => s.Query(sql, param, commandType, commandTimeout));
- }
+ protected DbContext(IDbConnectionFactory connectionFactory)
+ : base(connectionFactory)
+ { }
- public IEnumerable