@@ -34,16 +34,7 @@ public bool Equals(RootContainerCacheKey other)
34
34
return Equals ( other . type , type ) && Equals ( other . descriptors , descriptors ) ;
35
35
}
36
36
37
- public override bool Equals ( object obj )
38
- {
39
- if ( obj is null ) {
40
- return false ;
41
- }
42
- if ( obj . GetType ( ) != typeof ( RootContainerCacheKey ) ) {
43
- return false ;
44
- }
45
- return Equals ( ( RootContainerCacheKey ) obj ) ;
46
- }
37
+ public override bool Equals ( object obj ) => obj is RootContainerCacheKey other && Equals ( other ) ;
47
38
48
39
public override int GetHashCode ( ) => hashCode ;
49
40
@@ -126,20 +117,21 @@ private async ValueTask<StrongReferenceContainer> Prefetch(
126
117
127
118
var selectedFields = descriptors ;
128
119
var currentType = type ;
120
+ var currentKeyTypeReferenceType = currentKey . TypeReference . Type ;
129
121
var isKeyTypeExact = currentKey . HasExactType
130
- || currentKey . TypeReference . Type . IsLeaf
131
- || currentKey . TypeReference . Type == type ;
122
+ || currentKeyTypeReferenceType . IsLeaf
123
+ || currentKeyTypeReferenceType == type ;
132
124
if ( isKeyTypeExact ) {
133
- currentType = currentKey . TypeReference . Type ;
125
+ currentType = currentKeyTypeReferenceType ;
134
126
EnsureAllFieldsBelongToSpecifiedType ( descriptors , currentType ) ;
135
127
}
136
128
else {
137
129
ArgumentValidator . EnsureArgumentNotNull ( currentType , "type" ) ;
138
130
EnsureAllFieldsBelongToSpecifiedType ( descriptors , currentType ) ;
139
- _ = SetUpContainers ( currentKey , currentKey . TypeReference . Type ,
140
- PrefetchHelper . GetCachedDescriptorsForFieldsLoadedByDefault ( session . Domain , currentKey . TypeReference . Type ) ,
131
+ _ = SetUpContainers ( currentKey , currentKeyTypeReferenceType ,
132
+ PrefetchHelper . GetCachedDescriptorsForFieldsLoadedByDefault ( session . Domain , currentKeyTypeReferenceType ) ,
141
133
true , ownerState , true ) ;
142
- var hierarchyRoot = currentKey . TypeReference . Type ;
134
+ var hierarchyRoot = currentKeyTypeReferenceType ;
143
135
selectedFields = descriptors
144
136
. Where ( descriptor => descriptor . Field . DeclaringType != hierarchyRoot )
145
137
. ToList ( ) ;
@@ -215,16 +207,15 @@ public GraphContainer SetUpContainers(Key key, TypeInfo type,
215
207
{
216
208
var result = GetGraphContainer ( key , type , exactType ) ;
217
209
var areAnyColumns = false ;
218
- var haveColumnsBeenSet = canUseCache
219
- ? TrySetCachedColumnIndexes ( result , descriptors , state )
220
- : false ;
210
+ var haveColumnsBeenSet = canUseCache && TrySetCachedColumnIndexes ( result , descriptors , state ) ;
221
211
222
212
foreach ( var descriptor in descriptors ) {
223
- if ( descriptor . Field . IsEntity && descriptor . FetchFieldsOfReferencedEntity && ! type . IsAuxiliary ) {
213
+ var descriptorField = descriptor . Field ;
214
+ if ( descriptorField . IsEntity && descriptor . FetchFieldsOfReferencedEntity && ! type . IsAuxiliary ) {
224
215
areAnyColumns = true ;
225
216
result . RegisterReferencedEntityContainer ( state , descriptor ) ;
226
217
}
227
- else if ( descriptor . Field . IsEntitySet ) {
218
+ else if ( descriptorField . IsEntitySet ) {
228
219
result . RegisterEntitySetTask ( state , descriptor ) ;
229
220
}
230
221
else {
@@ -281,21 +272,18 @@ public void GetCachedColumnIndexes(TypeInfo type,
281
272
282
273
private static void EnsureKeyTypeCorrespondsToSpecifiedType ( Key key , TypeInfo type )
283
274
{
284
- if ( type == null || key . TypeReference . Type == type ) {
275
+ var keyTypeReferenceType = key . TypeReference . Type ;
276
+ if ( type == null || keyTypeReferenceType == type ) {
285
277
return ;
286
278
}
287
279
288
- if ( ! key . TypeReference . Type . IsInterface && ! type . IsInterface ) {
289
- if ( key . TypeReference . Type . Hierarchy == type . Hierarchy ) {
280
+ if ( ! keyTypeReferenceType . IsInterface && ! type . IsInterface ) {
281
+ if ( keyTypeReferenceType . Hierarchy == type . Hierarchy ) {
290
282
return ;
291
283
}
292
- else {
293
- throw new ArgumentException ( Strings . ExSpecifiedTypeHierarchyIsDifferentFromKeyHierarchy ) ;
294
- }
295
284
}
296
-
297
- if ( type . AllInterfaces . Contains ( key . TypeReference . Type )
298
- || key . TypeReference . Type . AllInterfaces . Contains ( type ) ) {
285
+ else if ( type . AllInterfaces . Contains ( keyTypeReferenceType )
286
+ || keyTypeReferenceType . AllInterfaces . Contains ( type ) ) {
299
287
return ;
300
288
}
301
289
@@ -304,9 +292,10 @@ private static void EnsureKeyTypeCorrespondsToSpecifiedType(Key key, TypeInfo ty
304
292
305
293
private static void EnsureAllFieldsBelongToSpecifiedType ( IReadOnlyList < PrefetchFieldDescriptor > descriptors , TypeInfo type )
306
294
{
307
- for ( var i = 0 ; i < descriptors . Count ; i ++ ) {
295
+ var typeUnderlyingType = type . UnderlyingType ;
296
+ for ( int i = 0 , count = descriptors . Count ; i < count ; i ++ ) {
308
297
var declaringType = descriptors [ i ] . Field . DeclaringType ;
309
- if ( type != declaringType && ! declaringType . UnderlyingType . IsAssignableFrom ( type . UnderlyingType ) ) {
298
+ if ( type != declaringType && ! declaringType . UnderlyingType . IsAssignableFrom ( typeUnderlyingType ) ) {
310
299
throw new InvalidOperationException (
311
300
string . Format ( Strings . ExFieldXIsNotDeclaredInTypeYOrInOneOfItsAncestors , descriptors [ i ] . Field , type ) ) ;
312
301
}
@@ -317,17 +306,13 @@ private GraphContainer GetGraphContainer(Key key, TypeInfo type, bool exactType)
317
306
graphContainers . GetValueOrDefault ( ( key , type ) )
318
307
?? ( graphContainers [ ( key , type ) ] = new GraphContainer ( key , type , exactType , this ) ) ;
319
308
320
- private static IEnumerable < ColumnInfo > ExtractColumns ( IEnumerable < PrefetchFieldDescriptor > descriptors )
321
- {
322
- foreach ( var descriptor in descriptors ) {
323
- var columns = descriptor . Field . IsStructure && ! descriptor . FetchLazyFields
324
- ? descriptor . Field . Columns . Where ( column => ! column . Field . IsLazyLoad )
325
- : descriptor . Field . Columns ;
326
- foreach ( var column in columns ) {
327
- yield return column ;
328
- }
329
- }
330
- }
309
+ private static IEnumerable < ColumnInfo > ExtractColumns ( IEnumerable < PrefetchFieldDescriptor > descriptors ) =>
310
+ descriptors . SelectMany ( static descriptor => {
311
+ var descriptorField = descriptor . Field ;
312
+ return descriptorField . IsStructure && ! descriptor . FetchLazyFields
313
+ ? descriptorField . Columns . Where ( static column => ! column . Field . IsLazyLoad )
314
+ : descriptorField . Columns ;
315
+ } ) ;
331
316
332
317
private bool TrySetCachedColumnIndexes (
333
318
GraphContainer container , IEnumerable < PrefetchFieldDescriptor > descriptors , EntityState state )
@@ -357,4 +342,4 @@ public PrefetchManager(Session session)
357
342
ColumnIndexesCacheSize , cacheEntry => cacheEntry . Key ) ;
358
343
}
359
344
}
360
- }
345
+ }
0 commit comments