Skip to content

Commit cc29867

Browse files
authored
Merge pull request #175 from DataObjects-NET/little-code-improvements
Minor code improvements
2 parents 40dddb5 + 59f0c24 commit cc29867

File tree

4 files changed

+129
-172
lines changed

4 files changed

+129
-172
lines changed

Orm/Xtensive.Orm/IoC/ServiceRegistration.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ namespace Xtensive.IoC
1919
[Serializable]
2020
public sealed class ServiceRegistration
2121
{
22-
private static readonly ConcurrentDictionary<ServiceRegistrationKey, ServiceRegistration[]> serviceRegistrationsByType =
22+
private static readonly ConcurrentDictionary<ServiceRegistrationKey, ServiceRegistration[]> ServiceRegistrationsByType =
2323
new ConcurrentDictionary<ServiceRegistrationKey, ServiceRegistration[]>();
2424

25+
private static readonly Func<ServiceRegistrationKey, ServiceRegistration[]> ServiceRegistrationsExtractor = ServiceRegistrationsExtractorImpl;
26+
2527
/// <summary>
2628
/// Gets the type of the service.
2729
/// </summary>
@@ -74,11 +76,12 @@ public static ServiceRegistration[] CreateAll(Type type) =>
7476
/// An array of <see cref="ServiceRegistration"/> objects.
7577
/// </returns>
7678
public static ServiceRegistration[] CreateAll(Type type, bool defaultOnly) =>
77-
serviceRegistrationsByType.GetOrAdd(new ServiceRegistrationKey(type, defaultOnly), ServiceRegistrationsExtractor);
79+
ServiceRegistrationsByType.GetOrAdd(new ServiceRegistrationKey(type, defaultOnly), ServiceRegistrationsExtractor);
7880

79-
private static readonly Func<ServiceRegistrationKey, ServiceRegistration[]> ServiceRegistrationsExtractor = ((Type type, bool defaultOnly) t) => {
81+
private static ServiceRegistration[] ServiceRegistrationsExtractorImpl(ServiceRegistrationKey t)
82+
{
8083
(var type, var defaultOnly) = t;
81-
ArgumentValidator.EnsureArgumentNotNull(type, "type");
84+
ArgumentValidator.EnsureArgumentNotNull(type, nameof(type));
8285
if (type.IsAbstract)
8386
return Array.Empty<ServiceRegistration>();
8487

@@ -90,8 +93,7 @@ public static ServiceRegistration[] CreateAll(Type type, bool defaultOnly) =>
9093
}
9194
}
9295
return registrations.ToArray();
93-
};
94-
96+
}
9597

9698
// Constructors
9799

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
}

0 commit comments

Comments
 (0)