@@ -146,9 +146,6 @@ protected virtual Expression InjectEntityMaterializer(Expression expression)
146
146
147
147
private class EntityMaterializerInjectingExpressionVisitor : ExpressionVisitor
148
148
{
149
- private readonly IDictionary < EntityShaperExpression , ParameterExpression > _entityCache
150
- = new Dictionary < EntityShaperExpression , ParameterExpression > ( ) ;
151
-
152
149
private static readonly ConstructorInfo _materializationContextConstructor
153
150
= typeof ( MaterializationContext ) . GetConstructors ( ) . Single ( ci => ci . GetParameters ( ) . Length == 2 ) ;
154
151
@@ -187,23 +184,10 @@ public EntityMaterializerInjectingExpressionVisitor(
187
184
public Expression Inject ( Expression expression )
188
185
{
189
186
var modifiedBody = Visit ( expression ) ;
190
- if ( _async )
191
- {
192
- var resultVariable = Expression . Variable ( typeof ( Task < > ) . MakeGenericType ( expression . Type ) , "result" ) ;
193
- _variables . Add ( resultVariable ) ;
194
- _expressions . Add ( Expression . Assign ( resultVariable ,
195
- Expression . Call (
196
- _taskFromResultMethodInfo . MakeGenericMethod ( expression . Type ) ,
197
- modifiedBody ) ) ) ;
198
- _expressions . Add ( resultVariable ) ;
199
- }
200
- else
201
- {
202
- var resultVariable = Expression . Variable ( expression . Type , "result" ) ;
203
- _variables . Add ( resultVariable ) ;
204
- _expressions . Add ( Expression . Assign ( resultVariable , modifiedBody ) ) ;
205
- _expressions . Add ( resultVariable ) ;
206
- }
187
+ _expressions . Add (
188
+ _async
189
+ ? Expression . Call ( _taskFromResultMethodInfo . MakeGenericMethod ( expression . Type ) , modifiedBody )
190
+ : modifiedBody ) ;
207
191
208
192
return Expression . Block ( _variables , _expressions ) ;
209
193
}
@@ -212,89 +196,7 @@ protected override Expression VisitExtension(Expression extensionExpression)
212
196
{
213
197
if ( extensionExpression is EntityShaperExpression entityShaperExpression )
214
198
{
215
- if ( _entityCache . TryGetValue ( entityShaperExpression , out var existingInstance ) )
216
- {
217
- return existingInstance ;
218
- }
219
-
220
- _currentEntityIndex ++ ;
221
- var entityType = entityShaperExpression . EntityType ;
222
- var valueBuffer = entityShaperExpression . ValueBufferExpression ;
223
- var primaryKey = entityType . FindPrimaryKey ( ) ;
224
-
225
- if ( _trackQueryResults && primaryKey == null )
226
- {
227
- throw new InvalidOperationException ( ) ;
228
- }
229
-
230
- var result = Expression . Variable ( entityType . ClrType , "result" + _currentEntityIndex ) ;
231
- _variables . Add ( result ) ;
232
-
233
- if ( _trackQueryResults )
234
- {
235
- var entry = Expression . Variable ( typeof ( InternalEntityEntry ) , "entry" + _currentEntityIndex ) ;
236
- var hasNullKey = Expression . Variable ( typeof ( bool ) , "hasNullKey" + _currentEntityIndex ) ;
237
- _variables . Add ( entry ) ;
238
- _variables . Add ( hasNullKey ) ;
239
-
240
- _expressions . Add (
241
- Expression . Assign (
242
- entry ,
243
- Expression . Call (
244
- Expression . MakeMemberAccess (
245
- QueryCompilationContext2 . QueryContextParameter ,
246
- _stateManagerMemberInfo ) ,
247
- _tryGetEntryMethodInfo ,
248
- Expression . Constant ( primaryKey ) ,
249
- Expression . NewArrayInit (
250
- typeof ( object ) ,
251
- primaryKey . Properties
252
- . Select ( p => _entityMaterializerSource . CreateReadValueExpression (
253
- entityShaperExpression . ValueBufferExpression ,
254
- typeof ( object ) ,
255
- p . GetIndex ( ) ,
256
- p ) ) ) ,
257
- Expression . Constant ( ! entityShaperExpression . Nullable ) ,
258
- hasNullKey ) ) ) ;
259
-
260
- _expressions . Add (
261
- Expression . Assign (
262
- result ,
263
- Expression . Condition (
264
- hasNullKey ,
265
- Expression . Constant ( null , entityType . ClrType ) ,
266
- Expression . Condition (
267
- Expression . NotEqual (
268
- entry ,
269
- Expression . Constant ( default ( InternalEntityEntry ) , typeof ( InternalEntityEntry ) ) ) ,
270
- Expression . Convert (
271
- Expression . MakeMemberAccess ( entry , _entityMemberInfo ) ,
272
- entityType . ClrType ) ,
273
- MaterializeEntity ( entityType , valueBuffer ) ) ) ) ) ;
274
- }
275
- else
276
- {
277
- _expressions . Add (
278
- Expression . Assign (
279
- result ,
280
- Expression . Condition (
281
- primaryKey . Properties
282
- . Select ( p =>
283
- Expression . Equal (
284
- _entityMaterializerSource . CreateReadValueExpression (
285
- entityShaperExpression . ValueBufferExpression ,
286
- typeof ( object ) ,
287
- p . GetIndex ( ) ,
288
- p ) ,
289
- Expression . Constant ( null ) ) )
290
- . Aggregate ( ( a , b ) => Expression . OrElse ( a , b ) ) ,
291
- Expression . Constant ( null , entityType . ClrType ) ,
292
- MaterializeEntity ( entityType , valueBuffer ) ) ) ) ;
293
- }
294
-
295
- _entityCache [ entityShaperExpression ] = result ;
296
-
297
- return result ;
199
+ return ProcessEntityShaper ( entityShaperExpression ) ;
298
200
}
299
201
300
202
if ( extensionExpression is EntityValuesExpression entityValuesExpression )
@@ -330,6 +232,81 @@ protected override Expression VisitExtension(Expression extensionExpression)
330
232
return base . VisitExtension ( extensionExpression ) ;
331
233
}
332
234
235
+ private Expression ProcessEntityShaper ( EntityShaperExpression entityShaperExpression )
236
+ {
237
+ _currentEntityIndex ++ ;
238
+ var expressions = new List < Expression > ( ) ;
239
+ var variables = new List < ParameterExpression > ( ) ;
240
+
241
+ var entityType = entityShaperExpression . EntityType ;
242
+ var valueBuffer = entityShaperExpression . ValueBufferExpression ;
243
+
244
+ var primaryKey = entityType . FindPrimaryKey ( ) ;
245
+
246
+ if ( _trackQueryResults && primaryKey == null )
247
+ {
248
+ throw new InvalidOperationException ( ) ;
249
+ }
250
+
251
+ if ( _trackQueryResults )
252
+ {
253
+ var entry = Expression . Variable ( typeof ( InternalEntityEntry ) , "entry" + _currentEntityIndex ) ;
254
+ var hasNullKey = Expression . Variable ( typeof ( bool ) , "hasNullKey" + _currentEntityIndex ) ;
255
+ variables . Add ( entry ) ;
256
+ variables . Add ( hasNullKey ) ;
257
+
258
+ expressions . Add (
259
+ Expression . Assign (
260
+ entry ,
261
+ Expression . Call (
262
+ Expression . MakeMemberAccess (
263
+ QueryCompilationContext2 . QueryContextParameter ,
264
+ _stateManagerMemberInfo ) ,
265
+ _tryGetEntryMethodInfo ,
266
+ Expression . Constant ( primaryKey ) ,
267
+ Expression . NewArrayInit (
268
+ typeof ( object ) ,
269
+ primaryKey . Properties
270
+ . Select ( p => _entityMaterializerSource . CreateReadValueExpression (
271
+ entityShaperExpression . ValueBufferExpression ,
272
+ typeof ( object ) ,
273
+ p . GetIndex ( ) ,
274
+ p ) ) ) ,
275
+ Expression . Constant ( ! entityShaperExpression . Nullable ) ,
276
+ hasNullKey ) ) ) ;
277
+
278
+ expressions . Add ( Expression . Condition (
279
+ hasNullKey ,
280
+ Expression . Constant ( null , entityType . ClrType ) ,
281
+ Expression . Condition (
282
+ Expression . NotEqual (
283
+ entry ,
284
+ Expression . Constant ( default ( InternalEntityEntry ) , typeof ( InternalEntityEntry ) ) ) ,
285
+ Expression . Convert (
286
+ Expression . MakeMemberAccess ( entry , _entityMemberInfo ) ,
287
+ entityType . ClrType ) ,
288
+ MaterializeEntity ( entityType , valueBuffer ) ) ) ) ;
289
+ }
290
+ else
291
+ {
292
+ expressions . Add ( Expression . Condition (
293
+ primaryKey . Properties
294
+ . Select ( p =>
295
+ Expression . Equal (
296
+ _entityMaterializerSource . CreateReadValueExpression (
297
+ entityShaperExpression . ValueBufferExpression ,
298
+ typeof ( object ) ,
299
+ p . GetIndex ( ) ,
300
+ p ) ,
301
+ Expression . Constant ( null ) ) )
302
+ . Aggregate ( ( a , b ) => Expression . OrElse ( a , b ) ) ,
303
+ Expression . Constant ( null , entityType . ClrType ) ,
304
+ MaterializeEntity ( entityType , valueBuffer ) ) ) ;
305
+ }
306
+
307
+ return Expression . Block ( variables , expressions ) ;
308
+ }
309
+
333
310
private Expression MaterializeEntity ( IEntityType entityType , Expression valueBuffer )
334
311
{
335
312
var expressions = new List < Expression > ( ) ;
0 commit comments