10
10
11
11
using System . Collections ;
12
12
using System . Collections . Generic ;
13
- using NHibernate . Cfg ;
14
- using NHibernate . Engine ;
13
+ using Antlr . Runtime . Misc ;
15
14
using NUnit . Framework ;
16
15
using NHibernate . Criterion ;
17
16
@@ -29,7 +28,7 @@ protected override string MappingsAssembly
29
28
30
29
protected override IList Mappings
31
30
{
32
- get { return new string [ ] { "EntityModeTest.Map.Basic.ProductLine.hbm.xml" } ; }
31
+ get { return new [ ] { "EntityModeTest.Map.Basic.ProductLine.hbm.xml" } ; }
33
32
}
34
33
35
34
public delegate IDictionary SingleCarQueryDelegate ( ISession session ) ;
@@ -110,5 +109,84 @@ public async Task ShouldWorkWithCriteriaAsync()
110
109
await ( t . CommitAsync ( cancellationToken ) ) ;
111
110
}
112
111
}
112
+
113
+ [ Test ]
114
+ public async Task ShouldWorkWithHQLAndGenericsAsync ( )
115
+ {
116
+ await ( TestLazyDynamicClassAsync (
117
+ s => s . CreateQuery ( "from ProductLine pl order by pl.Description" ) . UniqueResult < IDictionary < string , object > > ( ) ,
118
+ s => s . CreateQuery ( "from Model m" ) . List < IDictionary < string , object > > ( ) ) ) ;
119
+ }
120
+
121
+ [ Test ]
122
+ public async Task ShouldWorkWithCriteriaAndGenericsAsync ( )
123
+ {
124
+ await ( TestLazyDynamicClassAsync (
125
+ s => s . CreateCriteria ( "ProductLine" ) . AddOrder ( Order . Asc ( "Description" ) ) . UniqueResult < IDictionary < string , object > > ( ) ,
126
+ s => s . CreateCriteria ( "Model" ) . List < IDictionary < string , object > > ( ) ) ) ;
127
+ }
128
+
129
+ public async Task TestLazyDynamicClassAsync (
130
+ Func < ISession , IDictionary < string , object > > singleCarQueryHandler ,
131
+ Func < ISession , IList < IDictionary < string , object > > > allModelQueryHandler , CancellationToken cancellationToken = default ( CancellationToken ) )
132
+ {
133
+ using ( var s = OpenSession ( ) )
134
+ using ( var t = s . BeginTransaction ( ) )
135
+ {
136
+ var cars = new Dictionary < string , object > { [ "Description" ] = "Cars" } ;
137
+
138
+ var monaro = new Dictionary < string , object >
139
+ {
140
+ [ "ProductLine" ] = cars ,
141
+ [ "Name" ] = "Monaro" ,
142
+ [ "Description" ] = "Holden Monaro"
143
+ } ;
144
+
145
+ var hsv = new Dictionary < string , object >
146
+ {
147
+ [ "ProductLine" ] = cars ,
148
+ [ "Name" ] = "hsv" ,
149
+ [ "Description" ] = "Holden hsv"
150
+ } ;
151
+
152
+ var models = new List < IDictionary < string , object > > { monaro , hsv } ;
153
+
154
+ cars [ "Models" ] = models ;
155
+
156
+ await ( s . SaveAsync ( "ProductLine" , cars , cancellationToken ) ) ;
157
+ await ( t . CommitAsync ( cancellationToken ) ) ;
158
+ }
159
+
160
+ using ( var s = OpenSession ( ) )
161
+ using ( var t = s . BeginTransaction ( ) )
162
+ {
163
+ var cars = singleCarQueryHandler ( s ) ;
164
+ var models = ( IList < object > ) cars [ "Models" ] ;
165
+ Assert . That ( NHibernateUtil . IsInitialized ( models ) , Is . False ) ;
166
+ Assert . That ( models . Count , Is . EqualTo ( 2 ) ) ;
167
+ Assert . That ( NHibernateUtil . IsInitialized ( models ) , Is . True ) ;
168
+ s . Clear ( ) ;
169
+ var list = allModelQueryHandler ( s ) ;
170
+ foreach ( var dic in list )
171
+ {
172
+ Assert . That ( NHibernateUtil . IsInitialized ( dic [ "ProductLine" ] ) , Is . False ) ;
173
+ }
174
+ var model = list [ 0 ] ;
175
+ Assert . That ( ( ( IList < object > ) ( ( IDictionary < string , object > ) model [ "ProductLine" ] ) [ "Models" ] ) . Contains ( model ) , Is . True ) ;
176
+ s . Clear ( ) ;
177
+
178
+ await ( t . CommitAsync ( cancellationToken ) ) ;
179
+ }
180
+ }
181
+
182
+ protected override void OnTearDown ( )
183
+ {
184
+ using ( var s = OpenSession ( ) )
185
+ using ( var t = s . BeginTransaction ( ) )
186
+ {
187
+ s . Delete ( "from ProductLine" ) ;
188
+ t . Commit ( ) ;
189
+ }
190
+ }
113
191
}
114
- }
192
+ }
0 commit comments