@@ -27,28 +27,29 @@ namespace Xtensive.Orm
27
27
/// and finally, resolve <see cref="Key"/>s to <see cref="Entity">entities</see>.
28
28
/// </summary>
29
29
[ UsedImplicitly ( ImplicitUseTargetFlags . WithMembers ) ]
30
- public sealed class QueryEndpoint
30
+ public readonly struct QueryEndpoint : IEquatable < QueryEndpoint >
31
31
{
32
- private readonly Session session ;
33
-
34
- /// <summary>
35
- /// Gets outer <see cref="QueryEndpoint"/>.
36
- /// For root <see cref="QueryEndpoint"/> returns <see langword="null"/>.
37
- /// </summary>
38
- public QueryEndpoint Outer { get ; private set ; }
39
-
40
32
/// <summary>
41
33
/// Gets <see cref="IQueryProvider"/> implementation
42
34
/// for this session.
43
35
/// </summary>
44
36
public QueryProvider Provider { get ; }
45
37
38
+ private Session Session => Provider . Session ;
39
+
46
40
/// <summary>
47
41
/// Gets <see cref="IQueryRootBuilder"/> associated with this instance.
48
42
/// If <see cref="IQueryRootBuilder"/> is not set for this instance
49
43
/// returns <see langword="null"/>.
50
44
/// </summary>
51
- public IQueryRootBuilder RootBuilder { get ; private set ; }
45
+ public IQueryRootBuilder RootBuilder { get ; }
46
+
47
+ public bool Equals ( QueryEndpoint other ) =>
48
+ Provider == other . Provider && RootBuilder == other . RootBuilder ;
49
+
50
+ public override bool Equals ( object obj ) => obj is QueryEndpoint other && Equals ( other ) ;
51
+
52
+ public override int GetHashCode ( ) => HashCode . Combine ( Provider , RootBuilder ) ;
52
53
53
54
/// <summary>
54
55
/// The "starting point" for any LINQ query -
@@ -256,7 +257,7 @@ public IQueryable<FullTextMatch<T>> ContainsTable<T>(
256
257
257
258
/// <summary>
258
259
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
259
- /// in the current <see cref="session "/>.
260
+ /// in the current <see cref="Session "/>.
260
261
/// </summary>
261
262
/// <param name="key">The key to resolve.</param>
262
263
/// <returns>
@@ -277,7 +278,7 @@ public Entity Single(Key key)
277
278
278
279
/// <summary>
279
280
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
280
- /// in the current <see cref="session "/>.
281
+ /// in the current <see cref="Session "/>.
281
282
/// </summary>
282
283
/// <param name="key">The key to resolve.</param>
283
284
/// <returns>
@@ -298,7 +299,7 @@ public async ValueTask<Entity> SingleAsync(Key key, CancellationToken ct = defau
298
299
299
300
/// <summary>
300
301
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
301
- /// in the current <see cref="session "/>.
302
+ /// in the current <see cref="Session "/>.
302
303
/// </summary>
303
304
/// <param name="key">The key to resolve.</param>
304
305
/// <returns>
@@ -311,6 +312,7 @@ public Entity SingleOrDefault(Key key)
311
312
if ( key == null ) {
312
313
return null ;
313
314
}
315
+ var session = Session ;
314
316
using var tx = session . OpenAutoTransaction ( ) ;
315
317
EntityState state ;
316
318
if ( ! session . LookupStateInCache ( key , out state ) ) {
@@ -338,7 +340,7 @@ public Entity SingleOrDefault(Key key)
338
340
339
341
/// <summary>
340
342
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
341
- /// in the current <see cref="session "/>.
343
+ /// in the current <see cref="Session "/>.
342
344
/// </summary>
343
345
/// <param name="key">The key to resolve.</param>
344
346
/// <returns>
@@ -350,7 +352,8 @@ public async ValueTask<Entity> SingleOrDefaultAsync(Key key, CancellationToken c
350
352
if ( key == null ) {
351
353
return null ;
352
354
}
353
- using var tx = session . OpenAutoTransaction ( ) ;
355
+ var session = Session ;
356
+ await using var tx = session . OpenAutoTransaction ( ) ;
354
357
EntityState state ;
355
358
if ( ! session . LookupStateInCache ( key , out state ) ) {
356
359
if ( session . IsDebugEventLoggingEnabled ) {
@@ -377,7 +380,7 @@ public async ValueTask<Entity> SingleOrDefaultAsync(Key key, CancellationToken c
377
380
378
381
/// <summary>
379
382
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
380
- /// in the current <see cref="session "/>.
383
+ /// in the current <see cref="Session "/>.
381
384
/// </summary>
382
385
/// <typeparam name="T">Type of the entity.</typeparam>
383
386
/// <param name="key">The key to resolve.</param>
@@ -393,7 +396,7 @@ public T Single<T>(Key key)
393
396
394
397
/// <summary>
395
398
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="keyValues"/>
396
- /// in the current <see cref="session "/>.
399
+ /// in the current <see cref="Session "/>.
397
400
/// </summary>
398
401
/// <typeparam name="T">Type of the entity.</typeparam>
399
402
/// <param name="keyValues">Key values.</param>
@@ -409,7 +412,7 @@ public T Single<T>(params object[] keyValues)
409
412
410
413
/// <summary>
411
414
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
412
- /// in the current <see cref="session "/>.
415
+ /// in the current <see cref="Session "/>.
413
416
/// </summary>
414
417
/// <typeparam name="T">Type of the entity.</typeparam>
415
418
/// <param name="key">The key to resolve.</param>
@@ -424,7 +427,7 @@ [CanBeNull] public T SingleOrDefault<T>(Key key)
424
427
425
428
/// <summary>
426
429
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="keyValues"/>
427
- /// in the current <see cref="session "/>.
430
+ /// in the current <see cref="Session "/>.
428
431
/// </summary>
429
432
/// <typeparam name="T">Type of the entity.</typeparam>
430
433
/// <param name="keyValues">Key values.</param>
@@ -439,7 +442,7 @@ [CanBeNull] public T SingleOrDefault<T>(params object[] keyValues)
439
442
440
443
/// <summary>
441
444
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
442
- /// in the current <see cref="session "/>.
445
+ /// in the current <see cref="Session "/>.
443
446
/// </summary>
444
447
/// <typeparam name="T">Type of the entity.</typeparam>
445
448
/// <param name="key">The key to resolve.</param>
@@ -452,7 +455,7 @@ public async ValueTask<T> SingleAsync<T>(Key key, CancellationToken ct = default
452
455
453
456
/// <summary>
454
457
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="keyValues"/>
455
- /// in the current <see cref="session "/>.
458
+ /// in the current <see cref="Session "/>.
456
459
/// </summary>
457
460
/// <typeparam name="T">Type of the entity.</typeparam>
458
461
/// <param name="keyValues">Key values.</param>
@@ -468,7 +471,7 @@ public async ValueTask<T> SingleAsync<T>(object key1, object key2, CancellationT
468
471
469
472
/// <summary>
470
473
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="key"/>
471
- /// in the current <see cref="session "/>.
474
+ /// in the current <see cref="Session "/>.
472
475
/// </summary>
473
476
/// <typeparam name="T">Type of the entity.</typeparam>
474
477
/// <param name="key">The key to resolve.</param>
@@ -480,7 +483,7 @@ public async ValueTask<T> SingleOrDefaultAsync<T>(Key key, CancellationToken ct
480
483
481
484
/// <summary>
482
485
/// Resolves (gets) the <see cref="Entity"/> by the specified <paramref name="keyValues"/>
483
- /// in the current <see cref="session "/>.
486
+ /// in the current <see cref="Session "/>.
484
487
/// </summary>
485
488
/// <typeparam name="T">Type of the entity.</typeparam>
486
489
/// <param name="keyValues">Key values.</param>
@@ -501,7 +504,7 @@ public async ValueTask<T> SingleOrDefaultAsync<T>(object key1, object key2, Canc
501
504
public PrefetchQuery < T > Many < T > ( IEnumerable < Key > keys )
502
505
where T : class , IEntity
503
506
{
504
- return new PrefetchQuery < T > ( session , keys ) ;
507
+ return new PrefetchQuery < T > ( Session , keys ) ;
505
508
}
506
509
507
510
/// <summary>
@@ -516,16 +519,13 @@ public PrefetchQuery<T> Many<T, TElement>(IEnumerable<TElement> keys)
516
519
where T : class , IEntity
517
520
{
518
521
var elementType = typeof ( TElement ) ;
519
- Func < TElement , Key > selector ;
520
- if ( elementType == WellKnownTypes . ObjectArray ) {
521
- selector = e => Key . Create ( session . Domain , session . StorageNodeId , typeof ( T ) , TypeReferenceAccuracy . BaseType , ( object [ ] ) ( object ) e ) ;
522
- }
523
- else if ( WellKnownOrmTypes . Tuple . IsAssignableFrom ( elementType ) ) {
524
- selector = e => Key . Create ( session . Domain , session . StorageNodeId , typeof ( T ) , TypeReferenceAccuracy . BaseType , ( Tuple ) ( object ) e ) ;
525
- }
526
- else {
527
- selector = e => Key . Create ( session . Domain , session . StorageNodeId , typeof ( T ) , TypeReferenceAccuracy . BaseType , new object [ ] { e } ) ;
528
- }
522
+ var session = Session ;
523
+ Func < TElement , Key > selector =
524
+ elementType == WellKnownTypes . ObjectArray
525
+ ? e => Key . Create ( session . Domain , session . StorageNodeId , typeof ( T ) , TypeReferenceAccuracy . BaseType , ( object [ ] ) ( object ) e )
526
+ : WellKnownOrmTypes . Tuple . IsAssignableFrom ( elementType )
527
+ ? e => Key . Create ( session . Domain , session . StorageNodeId , typeof ( T ) , TypeReferenceAccuracy . BaseType , ( Tuple ) ( object ) e )
528
+ : e => Key . Create ( session . Domain , session . StorageNodeId , typeof ( T ) , TypeReferenceAccuracy . BaseType , new object [ ] { e } ) ;
529
529
530
530
return new PrefetchQuery < T > ( session , keys . Select ( selector ) ) ;
531
531
}
@@ -968,14 +968,15 @@ private Key GetKeyByValues<T>(object[] keyValues)
968
968
return entity . Key ;
969
969
}
970
970
}
971
+ var session = Session ;
971
972
return Key . Create ( session . Domain , session . StorageNodeId , typeof ( T ) , TypeReferenceAccuracy . BaseType , keyValues ) ;
972
973
}
973
974
974
975
private Expression BuildRootExpression ( Type elementType )
975
976
{
976
977
return RootBuilder != null
977
978
? RootBuilder . BuildRootExpression ( elementType )
978
- : session . Domain . RootCallExpressionsCache . GetOrAdd ( elementType , ( t ) => Expression . Call ( null , WellKnownMembers . Query . All . MakeGenericMethod ( t ) ) ) ;
979
+ : Session . Domain . RootCallExpressionsCache . GetOrAdd ( elementType , ( t ) => Expression . Call ( null , WellKnownMembers . Query . All . MakeGenericMethod ( t ) ) ) ;
979
980
}
980
981
981
982
private static void ThrowKeyNotFoundException ( Key key ) =>
@@ -990,15 +991,12 @@ internal QueryEndpoint(QueryProvider provider)
990
991
{
991
992
ArgumentNullException . ThrowIfNull ( provider ) ;
992
993
Provider = provider ;
993
- session = provider . Session ;
994
994
}
995
995
996
996
internal QueryEndpoint ( QueryEndpoint outerEndpoint , IQueryRootBuilder queryRootBuilder )
997
997
{
998
- ArgumentNullException . ThrowIfNull ( outerEndpoint ) ;
999
998
ArgumentNullException . ThrowIfNull ( queryRootBuilder ) ;
1000
999
Provider = outerEndpoint . Provider ;
1001
- session = outerEndpoint . session ;
1002
1000
RootBuilder = queryRootBuilder ;
1003
1001
}
1004
1002
}
0 commit comments