Skip to content

Commit 88d6b57

Browse files
committed
Code improvements
- nameofs instead of strings + no Resharper.UnusedMember - update formatting - get certain error messages when they needed
1 parent 40dddb5 commit 88d6b57

File tree

3 files changed

+121
-166
lines changed

3 files changed

+121
-166
lines changed

Orm/Xtensive.Orm/Orm/Linq/Materialization/ExpressionMaterializer.cs

Lines changed: 52 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Alexey Gamzov
@@ -90,8 +90,8 @@ protected override Expression VisitMarker(MarkerExpression expression)
9090
{
9191
var target = expression.Target;
9292
var processedTarget = Visit(target);
93-
if (expression.MarkerType!=MarkerType.None && (expression.MarkerType & MarkerType.Default)==MarkerType.None) {
94-
if (itemMaterializationContextParameter==null)
93+
if (expression.MarkerType != MarkerType.None && (expression.MarkerType & MarkerType.Default) == MarkerType.None) {
94+
if (itemMaterializationContextParameter == null)
9595
return processedTarget;
9696
var columns = ColumnGatherer.GetColumns(target, ColumnExtractionModes.Distinct | ColumnExtractionModes.Ordered).ToArray();
9797
var sequenceCheck = Expression.Call(MaterializationHelper.IsNullMethodInfo, tupleParameter, Expression.Constant(columns));
@@ -104,10 +104,8 @@ protected override Expression VisitMarker(MarkerExpression expression)
104104
protected override Expression VisitGroupingExpression(GroupingExpression groupingExpression)
105105
{
106106
// 1. Prepare subquery parameters.
107-
Parameter<Tuple> parameterOfTuple;
108-
Type elementType;
109-
ProjectionExpression projection;
110-
var translatedQuery = PrepareSubqueryParameters(groupingExpression, out parameterOfTuple, out elementType, out projection);
107+
var translatedQuery = PrepareSubqueryParameters(groupingExpression,
108+
out var parameterOfTuple, out var elementType, out var projection);
111109

112110
// 2. Create constructor
113111
var keyType = groupingExpression.KeyExpression.Type;
@@ -198,7 +196,7 @@ protected override Expression VisitFieldExpression(FieldExpression expression)
198196
var tupleExpression = GetTupleExpression(expression);
199197

200198
// Materialize non-owned field.
201-
if (expression.Owner==null || expression.UnderlyingProperty == null) {
199+
if (expression.Owner == null || expression.UnderlyingProperty == null) {
202200
if (expression.Field.IsEnum) {
203201
var underlyingType = Enum.GetUnderlyingType(expression.Type.StripNullable());
204202
if (expression.Field.IsNullable)
@@ -224,28 +222,27 @@ protected override Expression VisitFieldExpression(FieldExpression expression)
224222
return MaterializeThroughOwner(expression, tupleExpression);
225223
}
226224

227-
protected override Expression VisitLocalCollectionExpression(LocalCollectionExpression expression)
228-
{
229-
throw new NotSupportedException(String.Format(Strings.ExUnableToMaterializeBackLocalCollectionItem, expression.SourceExpression));
230-
}
225+
protected override Expression VisitLocalCollectionExpression(LocalCollectionExpression expression) =>
226+
throw new NotSupportedException(
227+
string.Format(Strings.ExUnableToMaterializeBackLocalCollectionItem, expression.SourceExpression));
231228

232229
protected override Expression VisitStructureFieldExpression(StructureFieldExpression expression)
233230
{
234231
var tupleExpression = GetTupleExpression(expression);
235232

236233
// Materialize non-owned structure.
237-
if (expression.Owner==null) {
234+
if (expression.Owner == null) {
238235
var typeInfo = expression.PersistentType;
239236
var tuplePrototype = typeInfo.TuplePrototype;
240237
var mappingInfo = expression.Fields
241238
.OfType<FieldExpression>()
242-
.Where(f => f.ExtendedType==ExtendedExpressionType.Field)
239+
.Where(f => f.ExtendedType == ExtendedExpressionType.Field)
243240
.OrderBy(f => f.Field.MappingInfo.Offset)
244241
.Select(f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
245242
.Distinct()
246243
.ToArray();
247244

248-
int[] columnMap = MaterializationHelper.CreateSingleSourceMap(tuplePrototype.Count, mappingInfo);
245+
var columnMap = MaterializationHelper.CreateSingleSourceMap(tuplePrototype.Count, mappingInfo);
249246

250247
var persistentTupleExpression = (Expression) Expression.Call(
251248
BuildPersistentTupleMethod,
@@ -266,7 +263,7 @@ protected override Expression VisitStructureFieldExpression(StructureFieldExpres
266263

267264
protected override Expression VisitConstructorExpression(ConstructorExpression expression)
268265
{
269-
var newExpression = expression.Constructor==null
266+
var newExpression = expression.Constructor == null
270267
? Expression.New(expression.Type) // Value type with default ctor (expression.Constructor is null in that case)
271268
: Expression.New(expression.Constructor, expression.ConstructorArguments.Select(Visit));
272269

@@ -275,9 +272,9 @@ protected override Expression VisitConstructorExpression(ConstructorExpression e
275272
return expression.NativeBindings.Count == 0
276273
? newExpression
277274
: (Expression) Expression.MemberInit(newExpression, expression
278-
.NativeBindings
279-
.Where(item => Translator.FilterBindings(item.Key, item.Key.Name, item.Value.Type))
280-
.Select(item => Expression.Bind(item.Key, Visit(item.Value))).Cast<MemberBinding>());
275+
.NativeBindings
276+
.Where(item => Translator.FilterBindings(item.Key, item.Key.Name, item.Value.Type))
277+
.Select(item => Expression.Bind(item.Key, Visit(item.Value))).Cast<MemberBinding>());
281278
}
282279

283280
protected override Expression VisitStructureExpression(StructureExpression expression)
@@ -288,13 +285,13 @@ protected override Expression VisitStructureExpression(StructureExpression expre
288285
var tuplePrototype = typeInfo.TuplePrototype;
289286
var mappingInfo = expression.Fields
290287
.OfType<FieldExpression>()
291-
.Where(f => f.ExtendedType==ExtendedExpressionType.Field)
288+
.Where(f => f.ExtendedType == ExtendedExpressionType.Field)
292289
.OrderBy(f => f.Field.MappingInfo.Offset)
293290
.Select(f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
294291
.Distinct()
295292
.ToArray();
296293

297-
int[] columnMap = MaterializationHelper.CreateSingleSourceMap(tuplePrototype.Count, mappingInfo);
294+
var columnMap = MaterializationHelper.CreateSingleSourceMap(tuplePrototype.Count, mappingInfo);
298295

299296
var persistentTupleExpression = (Expression) Expression.Call(
300297
BuildPersistentTupleMethod,
@@ -337,21 +334,23 @@ protected override Expression VisitEntityExpression(EntityExpression expression)
337334
/// <exception cref="InvalidOperationException">Unable to materialize Entity.</exception>
338335
private Expression CreateEntity(IEntityExpression expression, Expression tupleExpression)
339336
{
340-
int index;
341-
if (!entityRegistry.TryGetValue(expression, out index)) {
337+
if (!entityRegistry.TryGetValue(expression, out var index)) {
342338
index = entityRegistry.Count;
343339
entityRegistry.Add(expression, index);
344340
}
345341

346-
if (itemMaterializationContextParameter==null)
347-
throw new InvalidOperationException(String.Format(Strings.ExUnableToTranslateLambdaExpressionXBecauseItRequiresToMaterializeEntityOfTypeX, context.Translator.state.CurrentLambda, expression.PersistentType.UnderlyingType.FullName));
342+
if (itemMaterializationContextParameter == null)
343+
throw new InvalidOperationException(
344+
string.Format(Strings.ExUnableToTranslateLambdaExpressionXBecauseItRequiresToMaterializeEntityOfTypeX,
345+
context.Translator.state.CurrentLambda,
346+
expression.PersistentType.UnderlyingType.FullName));
348347

349-
var typeIdField = expression.Fields.SingleOrDefault(f => f.Name==WellKnown.TypeIdFieldName);
350-
int typeIdIndex = typeIdField==null ? -1 : typeIdField.Mapping.Offset;
348+
var typeIdField = expression.Fields.SingleOrDefault(f => f.Name == WellKnown.TypeIdFieldName);
349+
var typeIdIndex = typeIdField == null ? -1 : typeIdField.Mapping.Offset;
351350

352351
var mappingInfo = expression.Fields
353352
.OfType<FieldExpression>()
354-
.Where(f => f.ExtendedType==ExtendedExpressionType.Field)
353+
.Where(f => f.ExtendedType == ExtendedExpressionType.Field)
355354
.OrderBy(f => f.Field.MappingInfo.Offset)
356355
.Select(f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
357356
.Distinct()
@@ -384,13 +383,13 @@ private Expression CreateEntity(IEntityExpression expression, Expression tupleEx
384383
/// <exception cref="InvalidOperationException"><c>InvalidOperationException</c>.</exception>
385384
protected override Expression VisitEntityFieldExpression(EntityFieldExpression expression)
386385
{
387-
if (expression.Entity!=null)
386+
if (expression.Entity != null)
388387
return Visit(expression.Entity);
389388

390389
var tupleExpression = GetTupleExpression(expression);
391-
if (itemMaterializationContextParameter==null)
392-
return tupleExpression.MakeTupleAccess(expression.Type, expression.Mapping.Offset);
393-
return CreateEntity(expression, tupleExpression);
390+
return itemMaterializationContextParameter == null
391+
? tupleExpression.MakeTupleAccess(expression.Type, expression.Mapping.Offset)
392+
: CreateEntity(expression, tupleExpression);
394393
}
395394

396395
protected override Expression VisitEntitySetExpression(EntitySetExpression expression)
@@ -432,21 +431,18 @@ protected override Expression VisitUnary(UnaryExpression u)
432431
var index = tupleAccess.GetTupleAccessArgument();
433432
return tupleAccess.Object.MakeTupleAccess(u.Type, index);
434433
}
435-
if (operand != u.Operand) {
436-
return Expression.Convert(operand, u.Type);
437-
}
438-
return u;
434+
return operand != u.Operand ? Expression.Convert(operand, u.Type) : u;
439435
}
440436
return base.VisitUnary(u);
441437
}
442438

443439
protected override Expression VisitMemberAccess(MemberExpression m)
444440
{
445-
if (m.Expression!=null) {
446-
if ((ExtendedExpressionType) m.Expression.NodeType==ExtendedExpressionType.LocalCollection) {
441+
if (m.Expression != null) {
442+
if ((ExtendedExpressionType) m.Expression.NodeType == ExtendedExpressionType.LocalCollection) {
447443
return Visit((Expression) ((LocalCollectionExpression) m.Expression).Fields[m.Member]);
448444
}
449-
if (itemMaterializationContextParameter!=null
445+
if (itemMaterializationContextParameter != null
450446
&& string.Equals(nameof(Parameter<object>.Value), m.Member.Name, StringComparison.Ordinal)
451447
&& WellKnownOrmTypes.Parameter.IsAssignableFrom(m.Expression.Type)) {
452448
var parameterType = m.Expression.Type;
@@ -460,7 +456,7 @@ protected override Expression VisitMemberAccess(MemberExpression m)
460456
}
461457

462458
var expression = Visit(m.Expression);
463-
if (expression==m.Expression) {
459+
if (expression == m.Expression) {
464460
return m;
465461
}
466462

@@ -471,15 +467,12 @@ protected override Expression VisitMemberAccess(MemberExpression m)
471467

472468
#region Private Methods
473469

474-
private Expression MaterializeThroughOwner(Expression target, Expression tuple)
475-
{
476-
return MaterializeThroughOwner(target, tuple, false);
477-
}
470+
private Expression MaterializeThroughOwner(Expression target, Expression tuple) =>
471+
MaterializeThroughOwner(target, tuple, false);
478472

479473
private Expression MaterializeThroughOwner(Expression target, Expression tuple, bool defaultIfEmpty)
480474
{
481-
var field = target as FieldExpression;
482-
if (field!=null) {
475+
if (target is FieldExpression field) {
483476
defaultIfEmpty |= field.DefaultIfEmpty;
484477
var owner = field.Owner;
485478
var materializedOwner = MaterializeThroughOwner((Expression) owner, tuple, defaultIfEmpty);
@@ -492,20 +485,19 @@ private Expression MaterializeThroughOwner(Expression target, Expression tuple,
492485
}
493486
else
494487
fieldExpression = Expression.MakeMemberAccess(materializedOwner, field.Field.UnderlyingProperty);
495-
if (defaultIfEmpty) {
496-
return Expression.Condition(
497-
Expression.Equal(materializedOwner, Expression.Constant(null, materializedOwner.Type)),
498-
Expression.Call(MaterializationHelper.GetDefaultMethodInfo.MakeGenericMethod(field.Type)),
499-
fieldExpression);
500-
}
501-
return fieldExpression;
488+
return defaultIfEmpty
489+
? Expression.Condition(
490+
Expression.Equal(materializedOwner, Expression.Constant(null, materializedOwner.Type)),
491+
Expression.Call(MaterializationHelper.GetDefaultMethodInfo.MakeGenericMethod(field.Type)),
492+
fieldExpression)
493+
: fieldExpression;
502494
}
503495
return CreateEntity((EntityExpression) target, tuple);
504496
}
505497

506498
private Expression GetTupleExpression(ParameterizedExpression expression)
507499
{
508-
if (expression.OuterParameter==null)
500+
if (expression.OuterParameter == null)
509501
return tupleParameter;
510502

511503
var parameterOfTuple = context.GetTupleParameter(expression.OuterParameter);
@@ -517,7 +509,7 @@ private Expression GetTupleExpression(ParameterizedExpression expression)
517509
}
518510

519511
// Use ApplyParameter for RecordSet predicates
520-
if (itemMaterializationContextParameter==null) {
512+
if (itemMaterializationContextParameter == null) {
521513
var projectionExpression = context.Bindings[expression.OuterParameter];
522514
var applyParameter = context.GetApplyParameter(projectionExpression);
523515
var applyParameterExpression = Expression.Constant(applyParameter);
@@ -527,21 +519,14 @@ private Expression GetTupleExpression(ParameterizedExpression expression)
527519
return tupleParameter;
528520
}
529521

530-
// ReSharper disable UnusedMember.Local
531-
532522
private static Tuple BuildPersistentTuple(Tuple tuple, Tuple tuplePrototype, int[] mapping)
533523
{
534524
var result = tuplePrototype.CreateNew();
535525
tuple.CopyTo(result, mapping);
536526
return result;
537527
}
538528

539-
private static Tuple GetTupleSegment(Tuple tuple, in Segment<int> segment)
540-
{
541-
return tuple.GetSegment(segment).ToRegular();
542-
}
543-
544-
// ReSharper restore UnusedMember.Local
529+
private static Tuple GetTupleSegment(Tuple tuple, in Segment<int> segment) => tuple.GetSegment(segment).ToRegular();
545530

546531
#endregion
547532

@@ -561,12 +546,14 @@ private ExpressionMaterializer(ParameterExpression
561546

562547
static ExpressionMaterializer()
563548
{
549+
var thisType = typeof(ExpressionMaterializer);
550+
564551
ParameterContextProperty =
565552
WellKnownOrmTypes.ItemMaterializationContext.GetProperty(nameof(ItemMaterializationContext.ParameterContext));
566553
GetParameterValueMethod = WellKnownOrmTypes.ParameterContext.GetMethod(nameof(ParameterContext.GetValue));
567554
GetTupleParameterValueMethod = GetParameterValueMethod.MakeGenericMethod(WellKnownOrmTypes.Tuple);
568-
BuildPersistentTupleMethod = typeof (ExpressionMaterializer).GetMethod("BuildPersistentTuple", BindingFlags.NonPublic | BindingFlags.Static);
569-
GetTupleSegmentMethod = typeof (ExpressionMaterializer).GetMethod("GetTupleSegment", BindingFlags.NonPublic | BindingFlags.Static);
555+
BuildPersistentTupleMethod = thisType.GetMethod(nameof(BuildPersistentTuple), BindingFlags.NonPublic | BindingFlags.Static);
556+
GetTupleSegmentMethod = thisType.GetMethod(nameof(GetTupleSegment), BindingFlags.NonPublic | BindingFlags.Static);
570557
}
571558
}
572559
}

Orm/Xtensive.Orm/Orm/Linq/Materialization/ItemMaterializationContext.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

@@ -14,43 +14,34 @@ namespace Xtensive.Orm.Linq.Materialization
1414
{
1515
internal sealed class ItemMaterializationContext
1616
{
17-
public ParameterContext ParameterContext { get; }
18-
public static MethodInfo IsMaterializedMethodInfo { get; private set; }
19-
public static MethodInfo GetEntityMethodInfo { get; private set; }
20-
public static MethodInfo MaterializeMethodInfo { get; private set; }
21-
22-
public static System.Reflection.FieldInfo SessionFieldInfo { get; private set; }
17+
public static readonly MethodInfo IsMaterializedMethodInfo;
18+
public static readonly MethodInfo GetEntityMethodInfo;
19+
public static readonly MethodInfo MaterializeMethodInfo;
20+
public static readonly System.Reflection.FieldInfo SessionFieldInfo;
2321

2422
public readonly Session Session;
2523
public readonly MaterializationContext MaterializationContext;
2624

2725
private readonly TypeIdRegistry typeIdRegistry;
2826
private readonly Entity[] entities;
2927

30-
// ReSharper disable UnusedMember.Global
28+
public ParameterContext ParameterContext { get; }
3129

32-
public bool IsMaterialized(int index)
33-
{
34-
return entities[index]!=null;
35-
}
30+
public bool IsMaterialized(int index) => entities[index] != null;
3631

37-
public Entity GetEntity(int index)
38-
{
39-
return entities[index];
40-
}
32+
public Entity GetEntity(int index) => entities[index];
4133

4234
public Entity Materialize(int entityIndex, int typeIdIndex, TypeInfo type, Pair<int>[] entityColumns, Tuple tuple)
4335
{
4436
var result = entities[entityIndex];
45-
if (result!=null)
37+
if (result != null)
4638
return result;
4739

48-
TypeReferenceAccuracy accuracy;
49-
int typeId = EntityDataReader.ExtractTypeId(type, typeIdRegistry, tuple, typeIdIndex, out accuracy);
50-
if (typeId==TypeInfo.NoTypeId)
40+
var typeId = EntityDataReader.ExtractTypeId(type, typeIdRegistry, tuple, typeIdIndex, out var accuracy);
41+
if (typeId == TypeInfo.NoTypeId)
5142
return null;
5243

53-
bool canCache = accuracy==TypeReferenceAccuracy.ExactType;
44+
var canCache = accuracy==TypeReferenceAccuracy.ExactType;
5445
var materializationInfo = MaterializationContext.GetTypeMapping(entityIndex, type, typeId, entityColumns);
5546
Key key;
5647
var keyIndexes = materializationInfo.KeyIndexes;
@@ -62,7 +53,7 @@ public Entity Materialize(int entityIndex, int typeIdIndex, TypeInfo type, Pair<
6253
var keyTuple = materializationInfo.KeyTransform.Apply(TupleTransformType.TransformedTuple, tuple);
6354
key = KeyFactory.Materialize(Session.Domain, Session.StorageNodeId, materializationInfo.Type, keyTuple, accuracy, canCache, null);
6455
}
65-
if (accuracy==TypeReferenceAccuracy.ExactType) {
56+
if (accuracy == TypeReferenceAccuracy.ExactType) {
6657
var entityTuple = materializationInfo.Transform.Apply(TupleTransformType.Tuple, tuple);
6758
var entityState = Session.Handler.UpdateState(key, entityTuple);
6859
result = entityState.Entity;
@@ -74,8 +65,6 @@ public Entity Materialize(int entityIndex, int typeIdIndex, TypeInfo type, Pair<
7465
return result;
7566
}
7667

77-
// ReSharper restore UnusedMember.Global
78-
7968

8069
// Constructors
8170

0 commit comments

Comments
 (0)