Skip to content

Add missing ISession.Get(entityName, id, lockMode) #2378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1275/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public async Task RetrievingAsync()
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
}
s.Clear();
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
{
await (s.GetAsync<A>(typeof(A).FullName, savedId, LockMode.Upgrade));
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
}
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
{
await (s.CreateQuery("from A a where a.Id= :pid").SetLockMode("a", LockMode.Upgrade).SetParameter("pid", savedId).
Expand Down
7 changes: 7 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1275/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public void Retrieving()
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
}
s.Clear();
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
{
s.Get<A>(typeof(A).FullName, savedId, LockMode.Upgrade);
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
}
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
{
s.CreateQuery("from A a where a.Id= :pid").SetLockMode("a", LockMode.Upgrade).SetParameter("pid", savedId).
Expand Down
70 changes: 70 additions & 0 deletions src/NHibernate/Async/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,76 @@ namespace NHibernate
{
using System.Threading.Tasks;
using System.Threading;
public static partial class SessionExtensions
{

/// <summary>
/// Return the persistent instance of the given entity class with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <param name="session">The session.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="id">The entity identifier.</param>
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>A persistent instance, or <see langword="null" />.</returns>
public static Task<object> GetAsync(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
return
ReflectHelper
.CastOrThrow<SessionImpl>(session, "Get with entityName and lockMode")
.GetAsync(entityName, id, lockMode, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}

//NOTE: Keep it as extension
/// <summary>
/// Return the persistent instance of the given entity name with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <typeparam name="T">The entity class.</typeparam>
/// <param name="session">The session.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="id">The entity identifier.</param>
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>A persistent instance, or <see langword="null" />.</returns>
public static async Task<T> GetAsync<T>(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false);
}

//NOTE: Keep it as extension
/// <summary>
/// Return the persistent instance of the given entity name with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <typeparam name="T">The entity class.</typeparam>
/// <param name="session">The session.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="id">The entity identifier.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>A persistent instance, or <see langword="null" />.</returns>
public static async Task<T> GetAsync<T>(this ISession session, string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
return (T) await (session.GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false);
}
}

public partial interface ISession : IDisposable
{
Expand Down
40 changes: 40 additions & 0 deletions src/NHibernate/Async/IStatelessSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ namespace NHibernate
{
using System.Threading.Tasks;
using System.Threading;
public static partial class StatelessSessionExtensions
{

//NOTE: Keep it as extension
/// <summary>
/// Return the persistent instance of the given entity name with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <typeparam name="T">The entity class.</typeparam>
/// <param name="session">The session.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="id">The entity identifier.</param>
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>A persistent instance, or <see langword="null" />.</returns>
public static async Task<T> GetAsync<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false);
}

//NOTE: Keep it as extension
/// <summary>
/// Return the persistent instance of the given entity name with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <typeparam name="T">The entity class.</typeparam>
/// <param name="session">The session.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="id">The entity identifier.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>A persistent instance, or <see langword="null" />.</returns>
public static async Task<T> GetAsync<T>(this IStatelessSession session, string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
return (T) await (session.GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false);
}
}

public partial interface IStatelessSession : IDisposable
{
Expand Down
82 changes: 40 additions & 42 deletions src/NHibernate/Async/Impl/SessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -803,24 +803,21 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
return LoadAsync(entityClass.FullName, id, cancellationToken);
}

/// <inheritdoc />
public async Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
using (BeginProcess())
{
return (T)await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
}
return (T) await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<T> GetAsync<T>(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
using (BeginProcess())
{
return (T)await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false);
}
return (T) await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false);
}

/// <inheritdoc />
public Task<object> GetAsync(System.Type entityClass, object id, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
Expand All @@ -830,29 +827,50 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
return GetAsync(entityClass.FullName, id, cancellationToken);
}

/// <summary>
/// Load the data for the object with the specified id into a newly created object
/// using "for update", if supported. A new key will be assigned to the object.
/// This should return an existing proxy where appropriate.
///
/// If the object does not exist in the database, null is returned.
/// </summary>
/// <param name="clazz"></param>
/// <param name="id"></param>
/// <param name="lockMode"></param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns></returns>
public async Task<object> GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
/// <inheritdoc />
public Task<object> GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
return GetAsync(clazz.FullName, id, lockMode, cancellationToken);
}

/// <inheritdoc />
public async Task<object> GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
using (BeginProcess())
{
LoadEvent loadEvent = new LoadEvent(id, clazz.FullName, lockMode, this);
LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this);
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
//Note: AfterOperation call is skipped to avoid releasing the lock when outside of a transaction.
return loadEvent.Result;
}
}

/// <inheritdoc />
public async Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
using (BeginProcess())
{
LoadEvent loadEvent = new LoadEvent(id, entityName, null, this);
bool success = false;
try
{
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
success = true;
return loadEvent.Result;
}
finally
{
await (AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false);
}
}
}

public async Task<string> GetEntityNameAsync(object obj, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -882,26 +900,6 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
}
}

public async Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
using (BeginProcess())
{
LoadEvent loadEvent = new LoadEvent(id, entityName, false, this);
bool success = false;
try
{
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
success = true;
return loadEvent.Result;
}
finally
{
await (AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false);
}
}
}

/// <summary>
/// Load the data for the object with the specified id into a newly created object.
/// This is only called when lazily initializing a proxy.
Expand Down
31 changes: 8 additions & 23 deletions src/NHibernate/Async/Impl/StatelessSessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public async Task ManagedFlushAsync(CancellationToken cancellationToken)
}
}

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

/// <summary> Retrieve a entity.
///
/// <summary>
/// Retrieve an entity.
/// </summary>
/// <returns> a detached entity instance
/// </returns>
public async Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
using (BeginProcess())
{
return (T)await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
}
}

private Task<object> GetAsync(System.Type persistentClass, object id, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
return GetAsync(persistentClass.FullName, id, cancellationToken);
return (T) await (GetAsync(typeof(T).FullName, id, cancellationToken)).ConfigureAwait(false);
}

/// <summary>
/// Retrieve a entity, obtaining the specified lock mode.
/// Retrieve an entity, obtaining the specified lock mode.
/// </summary>
/// <returns> a detached entity instance </returns>
public async Task<object> GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
using (BeginProcess())
{
object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode, this, cancellationToken)).ConfigureAwait(false);
object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode ?? LockMode.None, this, cancellationToken)).ConfigureAwait(false);
if (temporaryPersistenceContext.IsLoadFinished)
{
temporaryPersistenceContext.Clear();
Expand All @@ -492,16 +480,13 @@ private Task<object> GetAsync(System.Type persistentClass, object id, Cancellati
}

/// <summary>
/// Retrieve a entity, obtaining the specified lock mode.
/// Retrieve an entity, obtaining the specified lock mode.
/// </summary>
/// <returns> a detached entity instance </returns>
public async Task<T> GetAsync<T>(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
using (BeginProcess())
{
return (T)await (GetAsync(typeof(T).FullName, id, lockMode, cancellationToken)).ConfigureAwait(false);
}
return (T) await (GetAsync(typeof(T).FullName, id, lockMode, cancellationToken)).ConfigureAwait(false);
}

/// <summary>
Expand Down
Loading