11using System ;
22using System . Data ;
33using System . Data . Common ;
4- using System . Linq ;
5- using System . Reflection ;
64using System . Threading ;
75using System . Threading . Tasks ;
86using Dapper ;
9- using Microsoft . Data . SqlClient ;
107
118namespace Dibix . Testing . Data
129{
@@ -37,35 +34,38 @@ private sealed class DapperDatabaseAccessorFactory : IDisposableDatabaseAccessor
3734 {
3835 private readonly RaiseErrorWithNoWaitBehavior _raiseErrorWithNoWaitBehavior ;
3936 private readonly int ? _defaultCommandTimeout ;
40- private readonly Lazy < SqlConnection > _connectionAccessor ;
37+ private readonly Lazy < DbConnection > _connectionAccessor ;
4138
4239 public DapperDatabaseAccessorFactory ( string connectionString , RaiseErrorWithNoWaitBehavior raiseErrorWithNoWaitBehavior , int ? defaultCommandTimeout )
4340 {
4441 _raiseErrorWithNoWaitBehavior = raiseErrorWithNoWaitBehavior ;
4542 _defaultCommandTimeout = defaultCommandTimeout ;
46- _connectionAccessor = new Lazy < SqlConnection > ( ( ) =>
43+ _connectionAccessor = new Lazy < DbConnection > ( ( ) =>
4744 {
48- SqlConnection connection = new SqlConnection ( connectionString ) ;
45+ DbConnection connection = CreateConnection ( connectionString ) ;
4946 connection . Open ( ) ;
5047 return connection ;
5148 } ) ;
5249 }
5350
5451 public IDatabaseAccessor Create ( )
5552 {
56- SqlConnection connection = _connectionAccessor . Value ;
53+ DbConnection connection = _connectionAccessor . Value ;
5754
55+ /*
5856 if (_raiseErrorWithNoWaitBehavior == RaiseErrorWithNoWaitBehavior.FireInfoMessageEventOnUserErrors)
5957 {
6058 connection.FireInfoMessageEventOnUserErrors = true;
6159 connection.InfoMessage += OnInfoMessage;
6260 }
61+ */
6362
6463 return new DapperDatabaseAccessor ( connection , _raiseErrorWithNoWaitBehavior , _defaultCommandTimeout ) ;
6564 }
6665
6766 // When FireInfoMessageEventOnUserErrors is true, errors will trigger an info message event aswell, without throwing an exception.
6867 // To restore the original behavior for errors, we have to throw ourselves.
68+ /*
6969 private static void OnInfoMessage(object sender, SqlInfoMessageEventArgs e)
7070 {
7171 bool isError = e.Errors.Cast<SqlError>().Aggregate(false, (current, sqlError) => current || sqlError.Class > 10);
@@ -85,6 +85,16 @@ private static void OnInfoMessage(object sender, SqlInfoMessageEventArgs e)
8585 // Unless they are of a severe exception type.
8686 throw new AccessViolationException(exception.Message, exception);
8787 }
88+ */
89+
90+ private static DbConnection CreateConnection ( string connectionString )
91+ {
92+ #if NETFRAMEWORK
93+ return new System . Data . SqlClient . SqlConnection ( connectionString ) ;
94+ #else
95+ return new Microsoft . Data . SqlClient . SqlConnection ( connectionString ) ;
96+ #endif
97+ }
8898
8999 void IDisposable . Dispose ( )
90100 {
@@ -98,7 +108,7 @@ private sealed class DapperDatabaseAccessor : Dapper.DapperDatabaseAccessor
98108 private readonly RaiseErrorWithNoWaitBehavior _raiseErrorWithNoWaitBehavior ;
99109 private readonly int ? _defaultCommandTimeout ;
100110
101- public DapperDatabaseAccessor ( DbConnection connection , RaiseErrorWithNoWaitBehavior raiseErrorWithNoWaitBehavior , int ? defaultCommandTimeout ) : base ( connection , defaultCommandTimeout : defaultCommandTimeout , sqlDataRecordAdapter : new MicrosoftSqlDataRecordAdapter ( ) )
111+ public DapperDatabaseAccessor ( DbConnection connection , RaiseErrorWithNoWaitBehavior raiseErrorWithNoWaitBehavior , int ? defaultCommandTimeout ) : base ( connection , defaultCommandTimeout : defaultCommandTimeout , sqlClientAdapter : CreateSqlClientAdapter ( connection ) )
102112 {
103113 _raiseErrorWithNoWaitBehavior = raiseErrorWithNoWaitBehavior ;
104114 _defaultCommandTimeout = defaultCommandTimeout ;
@@ -128,6 +138,15 @@ protected override void DisposeConnection()
128138 {
129139 // Will be disposed at the end of the test
130140 }
141+
142+ private static SqlClientAdapter CreateSqlClientAdapter ( DbConnection connection )
143+ {
144+ #if NETFRAMEWORK
145+ return new SystemSqlClientAdapter ( connection ) ;
146+ #else
147+ return new MicrosoftSqlClientAdapter ( connection ) ;
148+ #endif
149+ }
131150 }
132151 }
133152
0 commit comments