@@ -21,7 +21,7 @@ namespace Dapper.Tests
21
21
{
22
22
public class ParameterTests : TestBase
23
23
{
24
- public class DbParams : SqlMapper . IDynamicParameters , IEnumerable < IDbDataParameter >
24
+ public class DbDynamicParams : SqlMapper . IDynamicParameters , IEnumerable < IDbDataParameter >
25
25
{
26
26
private readonly List < IDbDataParameter > parameters = new List < IDbDataParameter > ( ) ;
27
27
public IEnumerator < IDbDataParameter > GetEnumerator ( ) { return parameters . GetEnumerator ( ) ; }
@@ -37,6 +37,21 @@ void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Id
37
37
command . Parameters . Add ( parameter ) ;
38
38
}
39
39
}
40
+
41
+ public class DbCustomParam : SqlMapper . ICustomQueryParameter
42
+ {
43
+ private readonly IDbDataParameter _sqlParameter ;
44
+
45
+ public DbCustomParam ( IDbDataParameter sqlParameter )
46
+ {
47
+ _sqlParameter = sqlParameter ;
48
+ }
49
+
50
+ public void AddParameter ( IDbCommand command , string name )
51
+ {
52
+ command . Parameters . Add ( _sqlParameter ) ;
53
+ }
54
+ }
40
55
41
56
private static List < Microsoft . SqlServer . Server . SqlDataRecord > CreateSqlDataRecordList ( IEnumerable < int > numbers )
42
57
{
@@ -672,9 +687,9 @@ public class HazSqlHierarchy
672
687
#endif
673
688
674
689
[ Fact ]
675
- public void TestCustomParameters ( )
690
+ public void TestDynamicParameters ( )
676
691
{
677
- var args = new DbParams {
692
+ var args = new DbDynamicParams {
678
693
new SqlParameter ( "foo" , 123 ) ,
679
694
new SqlParameter ( "bar" , "abc" )
680
695
} ;
@@ -684,7 +699,53 @@ public void TestCustomParameters()
684
699
Assert . Equal ( 123 , foo ) ;
685
700
Assert . Equal ( "abc" , bar ) ;
686
701
}
702
+
703
+ [ Fact ]
704
+ public void TestDynamicParametersReuse ( )
705
+ {
706
+ var args = new DbDynamicParams {
707
+ new SqlParameter ( "foo" , 123 ) ,
708
+ new SqlParameter ( "bar" , "abc" )
709
+ } ;
710
+ var result1 = connection . Query ( "select Foo=@foo, Bar=@bar" , args ) . Single ( ) ;
711
+ var result2 = connection . Query ( "select Foo=@foo, Bar=@bar" , args ) . Single ( ) ;
712
+ Assert . Equal ( 123 , result1 . Foo ) ;
713
+ Assert . Equal ( "abc" , result1 . Bar ) ;
714
+ Assert . Equal ( 123 , result2 . Foo ) ;
715
+ Assert . Equal ( "abc" , result2 . Bar ) ;
716
+ }
687
717
718
+
719
+ [ Fact ]
720
+ public void TestCustomParameter ( )
721
+ {
722
+ var args = new {
723
+ foo = new DbCustomParam ( new SqlParameter ( "foo" , 123 ) ) ,
724
+ bar = "abc"
725
+ } ;
726
+ var result = connection . Query ( "select Foo=@foo, Bar=@bar" , args ) . Single ( ) ;
727
+ int foo = result . Foo ;
728
+ string bar = result . Bar ;
729
+ Assert . Equal ( 123 , foo ) ;
730
+ Assert . Equal ( "abc" , bar ) ;
731
+ }
732
+
733
+ [ Fact ]
734
+ public void TestCustomParameterReuse ( )
735
+ {
736
+ var args = new {
737
+ foo = new DbCustomParam ( new SqlParameter ( "foo" , 123 ) ) ,
738
+ bar = "abc"
739
+ } ;
740
+ var result1 = connection . Query ( "select Foo=@foo, Bar=@bar" , args ) . Single ( ) ;
741
+ var result2 = connection . Query ( "select Foo=@foo, Bar=@bar" , args ) . Single ( ) ;
742
+ Assert . Equal ( 123 , result1 . Foo ) ;
743
+ Assert . Equal ( "abc" , result1 . Bar ) ;
744
+ Assert . Equal ( 123 , result2 . Foo ) ;
745
+ Assert . Equal ( "abc" , result2 . Bar ) ;
746
+ }
747
+
748
+
688
749
[ Fact ]
689
750
public void TestDynamicParamNullSupport ( )
690
751
{
0 commit comments