@@ -33,9 +33,11 @@ internal class ExpressionMaterializer : PersistentExpressionVisitor
3333 private static readonly MethodInfo GetParameterValueMethod ;
3434 private static readonly PropertyInfo ParameterContextProperty ;
3535 private static readonly MethodInfo GetTupleParameterValueMethod ;
36+ private static readonly ParameterExpression TupleParameter = Expression . Parameter ( WellKnownOrmTypes . Tuple , "tuple" ) ;
37+ private static readonly ParameterExpression MaterializationContextParameter = Expression . Parameter ( WellKnownOrmTypes . ItemMaterializationContext , "mc" ) ;
38+ private static readonly ConstantExpression TypeReferenceAccuracyConstantExpression = Expression . Constant ( TypeReferenceAccuracy . BaseType ) ;
3639
3740 private readonly TranslatorContext context ;
38- private readonly ParameterExpression tupleParameter ;
3941 private readonly ParameterExpression itemMaterializationContextParameter ;
4042 private readonly Dictionary < IEntityExpression , int > entityRegistry = new Dictionary < IEntityExpression , int > ( ) ;
4143 private readonly HashSet < Parameter < Tuple > > tupleParameters ;
@@ -52,19 +54,16 @@ internal class ExpressionMaterializer : PersistentExpressionVisitor
5254
5355 public static LambdaExpression MakeLambda ( Expression expression , TranslatorContext context )
5456 {
55- var tupleParameter = Expression . Parameter ( WellKnownOrmTypes . Tuple , "tuple" ) ;
56- var visitor = new ExpressionMaterializer ( tupleParameter , context , null , EnumerableUtils < Parameter < Tuple > > . Empty ) ;
57+ var visitor = new ExpressionMaterializer ( context , null , Enumerable . Empty < Parameter < Tuple > > ( ) ) ;
5758 var processedExpression = OwnerRemover . RemoveOwner ( expression ) ;
58- return FastExpression . Lambda ( visitor . Visit ( processedExpression ) , tupleParameter ) ;
59+ return FastExpression . Lambda ( visitor . Visit ( processedExpression ) , TupleParameter ) ;
5960 }
6061
61- public static MaterializationInfo MakeMaterialization ( ItemProjectorExpression projector , TranslatorContext context ,
62+ public static MaterializationInfo MakeMaterialization ( ItemProjectorExpression projector , TranslatorContext context ,
6263 IEnumerable < Parameter < Tuple > > tupleParameters )
6364 {
64- var tupleParameter = Expression . Parameter ( WellKnownOrmTypes . Tuple , "tuple" ) ;
65- var materializationContextParameter = Expression . Parameter ( WellKnownOrmTypes . ItemMaterializationContext , "mc" ) ;
66- var visitor = new ExpressionMaterializer ( tupleParameter , context , materializationContextParameter , tupleParameters ) ;
67- var lambda = FastExpression . Lambda ( visitor . Visit ( projector . Item ) , tupleParameter , materializationContextParameter ) ;
65+ var visitor = new ExpressionMaterializer ( context , MaterializationContextParameter , tupleParameters ) ;
66+ var lambda = FastExpression . Lambda ( visitor . Visit ( projector . Item ) , TupleParameter , MaterializationContextParameter ) ;
6867 var count = visitor . entityRegistry . Count ;
6968 return new MaterializationInfo ( count , lambda ) ;
7069 }
@@ -96,7 +95,7 @@ protected override Expression VisitMarker(MarkerExpression expression)
9695 if ( itemMaterializationContextParameter == null )
9796 return processedTarget ;
9897 var columns = ColumnGatherer . GetColumns ( target , ColumnExtractionModes . Distinct | ColumnExtractionModes . Ordered ) . ToArray ( ) ;
99- var sequenceCheck = Expression . Call ( MaterializationHelper . IsNullMethodInfo , tupleParameter , Expression . Constant ( columns ) ) ;
98+ var sequenceCheck = Expression . Call ( MaterializationHelper . IsNullMethodInfo , TupleParameter , Expression . Constant ( columns ) ) ;
10099 var throwException = Expression . Convert ( Expression . Call ( MaterializationHelper . ThrowEmptySequenceExceptionMethodInfo ) , target . Type ) ;
101100 return Expression . Condition ( sequenceCheck , throwException , processedTarget ) ;
102101 }
@@ -129,7 +128,7 @@ protected override Expression VisitGroupingExpression(GroupingExpression groupin
129128 Expression . Constant ( projection ) ,
130129 Expression . Constant ( translatedQuery ) ,
131130 Expression . Constant ( parameterOfTuple ) ,
132- tupleParameter ,
131+ TupleParameter ,
133132 keyMaterializer ,
134133 itemMaterializationContextParameter ) ;
135134
@@ -154,7 +153,7 @@ protected override Expression VisitSubQueryExpression(SubQueryExpression subQuer
154153 Expression . Constant ( projection ) ,
155154 Expression . Constant ( translatedQuery ) ,
156155 Expression . Constant ( parameterOfTuple ) ,
157- tupleParameter ,
156+ TupleParameter ,
158157 itemMaterializationContextParameter ) ;
159158
160159 return Expression . Convert ( resultExpression , subQueryExpression . Type ) ;
@@ -184,15 +183,15 @@ private TranslatedQuery PrepareSubqueryParameters(SubQueryExpression subQueryExp
184183 var itemProjector = new ItemProjectorExpression ( newItemProjectorBody , newDataSource , subQueryExpression . ProjectionExpression . ItemProjector . Context ) ;
185184 parameterOfTuple = context . GetTupleParameter ( subQueryExpression . OuterParameter ) ;
186185
187- // 2. Add only parameter<tuple>. Tuple value will be assigned
186+ // 2. Add only parameter<tuple>. Tuple value will be assigned
188187 // at the moment of materialization in SubQuery constructor
189188 projection = new ProjectionExpression (
190189 subQueryExpression . ProjectionExpression . Type ,
191190 itemProjector ,
192191 subQueryExpression . ProjectionExpression . TupleParameterBindings ,
193192 subQueryExpression . ProjectionExpression . ResultAccessMethod ) ;
194193
195- // 3. Make translation
194+ // 3. Make translation
196195 elementType = subQueryExpression . ProjectionExpression . ItemProjector . Item . Type ;
197196 var translateMethod = Translator . TranslateMethod ;
198197 return ( TranslatedQuery ) translateMethod . Invoke (
@@ -278,8 +277,8 @@ protected override Expression VisitConstructorExpression(ConstructorExpression e
278277
279278 var realBindings = expression . NativeBindings ;
280279
281- return expression . NativeBindings . Count == 0
282- ? newExpression
280+ return expression . NativeBindings . Count == 0
281+ ? newExpression
283282 : ( Expression ) Expression . MemberInit ( newExpression , expression
284283 . NativeBindings
285284 . Where ( item => Translator . FilterBindings ( item . Key , item . Key . Name , item . Value . Type ) )
@@ -330,7 +329,7 @@ protected override Expression VisitKeyExpression(KeyExpression expression)
330329 Expression . Field ( itemMaterializationContextParameter , ItemMaterializationContext . SessionFieldInfo ) ,
331330 WellKnownMembers . SessionNodeId ) ,
332331 Expression . Constant ( expression . EntityType ) ,
333- Expression . Constant ( TypeReferenceAccuracy . BaseType ) ,
332+ TypeReferenceAccuracyConstantExpression ,
334333 tupleExpression ) ;
335334 }
336335
@@ -507,7 +506,7 @@ private Expression MaterializeThroughOwner(Expression target, Expression tuple,
507506 private Expression GetTupleExpression ( ParameterizedExpression expression )
508507 {
509508 if ( expression . OuterParameter == null )
510- return tupleParameter ;
509+ return TupleParameter ;
511510
512511 var parameterOfTuple = context . GetTupleParameter ( expression . OuterParameter ) ;
513512 if ( tupleParameters . Contains ( parameterOfTuple ) ) {
@@ -525,7 +524,7 @@ private Expression GetTupleExpression(ParameterizedExpression expression)
525524 return Expression . Property ( applyParameterExpression , WellKnownMembers . ApplyParameterValue ) ;
526525 }
527526
528- return tupleParameter ;
527+ return TupleParameter ;
529528 }
530529
531530 private static Tuple BuildPersistentTuple ( Tuple tuple , Tuple tuplePrototype , int [ ] mapping )
@@ -541,14 +540,12 @@ private static Tuple BuildPersistentTuple(Tuple tuple, Tuple tuplePrototype, int
541540
542541 // Constructors
543542
544- private ExpressionMaterializer ( ParameterExpression
545- tupleParameter ,
543+ private ExpressionMaterializer (
546544 TranslatorContext context ,
547545 ParameterExpression itemMaterializationContextParameter ,
548546 IEnumerable < Parameter < Tuple > > tupleParameters )
549547 {
550548 this . itemMaterializationContextParameter = itemMaterializationContextParameter ;
551- this . tupleParameter = tupleParameter ;
552549 this . context = context ;
553550 this . tupleParameters = new HashSet < Parameter < Tuple > > ( tupleParameters ) ;
554551 }
0 commit comments