2
2
using System . Data . SqlClient ;
3
3
using System . Diagnostics ;
4
4
using System . Reflection ;
5
+ using NHibernate . Util ;
5
6
6
7
namespace NHibernate . AdoNet
7
8
{
8
- using Action = System . Action ;
9
9
using SqlCommand = System . Data . SqlClient . SqlCommand ;
10
10
11
11
/// <summary>
12
- /// Expose the batch functionality in ADO.Net 2 .0
12
+ /// Expose the batch functionality in ADO.Net 4 .0
13
13
/// Microsoft in its wisdom decided to make my life hard and mark it internal.
14
14
/// Through the use of Reflection and some delegates magic, I opened up the functionality.
15
15
///
@@ -18,35 +18,37 @@ namespace NHibernate.AdoNet
18
18
public class SqlClientSqlCommandSet : IDisposable
19
19
{
20
20
private static readonly System . Type sqlCmdSetType ;
21
+ private static readonly Action < object , SqlConnection > connectionSetter ;
22
+ private static readonly Action < object , SqlTransaction > transactionSetter ;
23
+ private static readonly Action < object , int > commandTimeoutSetter ;
24
+ private static readonly Func < object , SqlConnection > connectionGetter ;
25
+ private static readonly Func < object , SqlCommand > batchCommandGetter ;
26
+ private static readonly Action < object , SqlCommand > doAppend ;
27
+ private static readonly Func < object , int > doExecuteNonQuery ;
28
+ private static readonly Action < object > doDispose ;
29
+
21
30
private readonly object instance ;
22
- private readonly Action < SqlConnection > connectionSetter ;
23
- private readonly Action < SqlTransaction > transactionSetter ;
24
- private readonly Action < int > commandTimeoutSetter ;
25
- private readonly Func < SqlConnection > connectionGetter ;
26
- private readonly Func < SqlCommand > commandGetter ;
27
- private readonly Action < SqlCommand > doAppend ;
28
- private readonly Func < int > doExecuteNonQuery ;
29
- private readonly Action doDispose ;
30
31
private int countOfCommands ;
31
32
32
33
static SqlClientSqlCommandSet ( )
33
34
{
34
- var sysData = Assembly . Load ( "System.Data, Version=2 .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ) ;
35
+ var sysData = Assembly . Load ( "System.Data, Version=4 .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ) ;
35
36
sqlCmdSetType = sysData . GetType ( "System.Data.SqlClient.SqlCommandSet" ) ;
36
37
Debug . Assert ( sqlCmdSetType != null , "Could not find SqlCommandSet!" ) ;
38
+
39
+ connectionSetter = DelegateHelper . BuildPropertySetter < SqlConnection > ( sqlCmdSetType , "Connection" ) ;
40
+ connectionGetter = DelegateHelper . BuildPropertyGetter < SqlConnection > ( sqlCmdSetType , "Connection" ) ;
41
+ transactionSetter = DelegateHelper . BuildPropertySetter < SqlTransaction > ( sqlCmdSetType , "Transaction" ) ;
42
+ commandTimeoutSetter = DelegateHelper . BuildPropertySetter < int > ( sqlCmdSetType , "CommandTimeout" ) ;
43
+ batchCommandGetter = DelegateHelper . BuildPropertyGetter < SqlCommand > ( sqlCmdSetType , "BatchCommand" ) ;
44
+ doAppend = DelegateHelper . BuildAction < SqlCommand > ( sqlCmdSetType , "Append" ) ;
45
+ doExecuteNonQuery = DelegateHelper . BuildFunc < int > ( sqlCmdSetType , "ExecuteNonQuery" ) ;
46
+ doDispose = DelegateHelper . BuildAction ( sqlCmdSetType , "Dispose" ) ;
37
47
}
38
48
39
49
public SqlClientSqlCommandSet ( )
40
50
{
41
51
instance = Activator . CreateInstance ( sqlCmdSetType , true ) ;
42
- connectionSetter = ( Action < SqlConnection > ) Delegate . CreateDelegate ( typeof ( Action < SqlConnection > ) , instance , "set_Connection" ) ;
43
- transactionSetter = ( Action < SqlTransaction > ) Delegate . CreateDelegate ( typeof ( Action < SqlTransaction > ) , instance , "set_Transaction" ) ;
44
- commandTimeoutSetter = ( Action < int > ) Delegate . CreateDelegate ( typeof ( Action < int > ) , instance , "set_CommandTimeout" ) ;
45
- connectionGetter = ( Func < SqlConnection > ) Delegate . CreateDelegate ( typeof ( Func < SqlConnection > ) , instance , "get_Connection" ) ;
46
- commandGetter = ( Func < SqlCommand > ) Delegate . CreateDelegate ( typeof ( Func < SqlCommand > ) , instance , "get_BatchCommand" ) ;
47
- doAppend = ( Action < SqlCommand > ) Delegate . CreateDelegate ( typeof ( Action < SqlCommand > ) , instance , "Append" ) ;
48
- doExecuteNonQuery = ( Func < int > ) Delegate . CreateDelegate ( typeof ( Func < int > ) , instance , "ExecuteNonQuery" ) ;
49
- doDispose = ( Action ) Delegate . CreateDelegate ( typeof ( Action ) , instance , "Dispose" ) ;
50
52
}
51
53
52
54
/// <summary>
@@ -56,7 +58,7 @@ public SqlClientSqlCommandSet()
56
58
public void Append ( SqlCommand command )
57
59
{
58
60
AssertHasParameters ( command ) ;
59
- doAppend ( command ) ;
61
+ doAppend ( instance , command ) ;
60
62
countOfCommands ++ ;
61
63
}
62
64
@@ -79,7 +81,7 @@ private static void AssertHasParameters(SqlCommand command)
79
81
/// </summary>
80
82
public SqlCommand BatchCommand
81
83
{
82
- get { return commandGetter ( ) ; }
84
+ get { return batchCommandGetter ( instance ) ; }
83
85
}
84
86
85
87
/// <summary>
@@ -104,23 +106,23 @@ public int ExecuteNonQuery()
104
106
105
107
if ( CountOfCommands == 0 )
106
108
return 0 ;
107
- return doExecuteNonQuery ( ) ;
109
+ return doExecuteNonQuery ( instance ) ;
108
110
}
109
111
110
112
public SqlConnection Connection
111
113
{
112
- get { return connectionGetter ( ) ; }
113
- set { connectionSetter ( value ) ; }
114
+ get { return connectionGetter ( instance ) ; }
115
+ set { connectionSetter ( instance , value ) ; }
114
116
}
115
117
116
118
public SqlTransaction Transaction
117
119
{
118
- set { transactionSetter ( value ) ; }
120
+ set { transactionSetter ( instance , value ) ; }
119
121
}
120
122
121
123
public int CommandTimeout
122
124
{
123
- set { commandTimeoutSetter ( value ) ; }
125
+ set { commandTimeoutSetter ( instance , value ) ; }
124
126
}
125
127
126
128
///<summary>
@@ -129,7 +131,7 @@ public int CommandTimeout
129
131
///<filterpriority>2</filterpriority>
130
132
public void Dispose ( )
131
133
{
132
- doDispose ( ) ;
134
+ doDispose ( instance ) ;
133
135
}
134
136
}
135
137
}
0 commit comments