Skip to content

Commit bc34f14

Browse files
committed
Split extension and small refactoring
1 parent 0b16d0a commit bc34f14

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

src/NHibernate/Async/IStatelessSession.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,29 @@ public static partial class StatelessSessionExtensions
3838
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
3939
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
4040
/// <returns>A persistent instance, or <see langword="null" />.</returns>
41-
public static async Task<T> GetAsync<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken))
41+
public static async Task<T> GetAsync<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
4242
{
4343
cancellationToken.ThrowIfCancellationRequested();
4444
return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false);
4545
}
46+
47+
//NOTE: Keep it as extension
48+
/// <summary>
49+
/// Return the persistent instance of the given entity name with the given identifier, or null
50+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
51+
/// already associated with the session, return that instance or proxy.)
52+
/// </summary>
53+
/// <typeparam name="T">The entity class.</typeparam>
54+
/// <param name="session">The session.</param>
55+
/// <param name="entityName">The entity name.</param>
56+
/// <param name="id">The entity identifier.</param>
57+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
58+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
59+
public static async Task<T> GetAsync<T>(this IStatelessSession session, string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
60+
{
61+
cancellationToken.ThrowIfCancellationRequested();
62+
return (T) await (session.GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false);
63+
}
4664
}
4765

4866
public partial interface IStatelessSession : IDisposable

src/NHibernate/Async/Impl/StatelessSessionImpl.cs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public async Task ManagedFlushAsync(CancellationToken cancellationToken)
439439
}
440440
}
441441

442-
/// <summary> Retrieve a entity. </summary>
442+
/// <summary> Retrieve an entity. </summary>
443443
/// <returns> a detached entity instance </returns>
444444
public Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
445445
{
@@ -450,39 +450,27 @@ public async Task ManagedFlushAsync(CancellationToken cancellationToken)
450450
return GetAsync(entityName, id, LockMode.None, cancellationToken);
451451
}
452452

453-
/// <summary> Retrieve a entity.
454-
///
453+
/// <summary>
454+
/// Retrieve an entity.
455455
/// </summary>
456456
/// <returns> a detached entity instance
457457
/// </returns>
458458
public async Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
459459
{
460460
cancellationToken.ThrowIfCancellationRequested();
461-
using (BeginProcess())
462-
{
463-
return (T)await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
464-
}
465-
}
466-
467-
private Task<object> GetAsync(System.Type persistentClass, object id, CancellationToken cancellationToken)
468-
{
469-
if (cancellationToken.IsCancellationRequested)
470-
{
471-
return Task.FromCanceled<object>(cancellationToken);
472-
}
473-
return GetAsync(persistentClass.FullName, id, cancellationToken);
461+
return (T) await (GetAsync(typeof(T).FullName, id, cancellationToken)).ConfigureAwait(false);
474462
}
475463

476464
/// <summary>
477-
/// Retrieve a entity, obtaining the specified lock mode.
465+
/// Retrieve an entity, obtaining the specified lock mode.
478466
/// </summary>
479467
/// <returns> a detached entity instance </returns>
480468
public async Task<object> GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
481469
{
482470
cancellationToken.ThrowIfCancellationRequested();
483471
using (BeginProcess())
484472
{
485-
object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode, this, cancellationToken)).ConfigureAwait(false);
473+
object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode ?? LockMode.None, this, cancellationToken)).ConfigureAwait(false);
486474
if (temporaryPersistenceContext.IsLoadFinished)
487475
{
488476
temporaryPersistenceContext.Clear();
@@ -492,16 +480,13 @@ private Task<object> GetAsync(System.Type persistentClass, object id, Cancellati
492480
}
493481

494482
/// <summary>
495-
/// Retrieve a entity, obtaining the specified lock mode.
483+
/// Retrieve an entity, obtaining the specified lock mode.
496484
/// </summary>
497485
/// <returns> a detached entity instance </returns>
498486
public async Task<T> GetAsync<T>(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
499487
{
500488
cancellationToken.ThrowIfCancellationRequested();
501-
using (BeginProcess())
502-
{
503-
return (T)await (GetAsync(typeof(T).FullName, id, lockMode, cancellationToken)).ConfigureAwait(false);
504-
}
489+
return (T) await (GetAsync(typeof(T).FullName, id, lockMode, cancellationToken)).ConfigureAwait(false);
505490
}
506491

507492
/// <summary>

src/NHibernate/IStatelessSession.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,26 @@ public static ITransaction GetCurrentTransaction(this IStatelessSession session)
4444
/// <param name="id">The entity identifier.</param>
4545
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
4646
/// <returns>A persistent instance, or <see langword="null" />.</returns>
47-
public static T Get<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode = null)
47+
public static T Get<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode)
4848
{
4949
return (T) session.Get(entityName, id, lockMode);
5050
}
51+
52+
//NOTE: Keep it as extension
53+
/// <summary>
54+
/// Return the persistent instance of the given entity name with the given identifier, or null
55+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
56+
/// already associated with the session, return that instance or proxy.)
57+
/// </summary>
58+
/// <typeparam name="T">The entity class.</typeparam>
59+
/// <param name="session">The session.</param>
60+
/// <param name="entityName">The entity name.</param>
61+
/// <param name="id">The entity identifier.</param>
62+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
63+
public static T Get<T>(this IStatelessSession session, string entityName, object id)
64+
{
65+
return (T) session.Get(entityName, id);
66+
}
5167
}
5268

5369
/// <summary>

src/NHibernate/Impl/StatelessSessionImpl.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -539,40 +539,32 @@ public void Delete(string entityName, object entity)
539539
}
540540
}
541541

542-
/// <summary> Retrieve a entity. </summary>
542+
/// <summary> Retrieve an entity. </summary>
543543
/// <returns> a detached entity instance </returns>
544544
public object Get(string entityName, object id)
545545
{
546546
return Get(entityName, id, LockMode.None);
547547
}
548548

549-
/// <summary> Retrieve a entity.
550-
///
549+
/// <summary>
550+
/// Retrieve an entity.
551551
/// </summary>
552552
/// <returns> a detached entity instance
553553
/// </returns>
554554
public T Get<T>(object id)
555555
{
556-
using (BeginProcess())
557-
{
558-
return (T)Get(typeof(T), id);
559-
}
560-
}
561-
562-
private object Get(System.Type persistentClass, object id)
563-
{
564-
return Get(persistentClass.FullName, id);
556+
return (T) Get(typeof(T).FullName, id);
565557
}
566558

567559
/// <summary>
568-
/// Retrieve a entity, obtaining the specified lock mode.
560+
/// Retrieve an entity, obtaining the specified lock mode.
569561
/// </summary>
570562
/// <returns> a detached entity instance </returns>
571563
public object Get(string entityName, object id, LockMode lockMode)
572564
{
573565
using (BeginProcess())
574566
{
575-
object result = Factory.GetEntityPersister(entityName).Load(id, null, lockMode, this);
567+
object result = Factory.GetEntityPersister(entityName).Load(id, null, lockMode ?? LockMode.None, this);
576568
if (temporaryPersistenceContext.IsLoadFinished)
577569
{
578570
temporaryPersistenceContext.Clear();
@@ -582,15 +574,12 @@ public object Get(string entityName, object id, LockMode lockMode)
582574
}
583575

584576
/// <summary>
585-
/// Retrieve a entity, obtaining the specified lock mode.
577+
/// Retrieve an entity, obtaining the specified lock mode.
586578
/// </summary>
587579
/// <returns> a detached entity instance </returns>
588580
public T Get<T>(object id, LockMode lockMode)
589581
{
590-
using (BeginProcess())
591-
{
592-
return (T)Get(typeof(T).FullName, id, lockMode);
593-
}
582+
return (T) Get(typeof(T).FullName, id, lockMode);
594583
}
595584

596585
/// <summary>

0 commit comments

Comments
 (0)