Skip to content

Commit 650e8c8

Browse files
committed
Apply same logic for IsNotTransient and IsTransient
1 parent 372dc28 commit 650e8c8

File tree

3 files changed

+20
-48
lines changed

3 files changed

+20
-48
lines changed

src/NHibernate/Async/Engine/ForeignKeys.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ private async Task<bool> IsNullifiableAsync(string entityName, object obj, Cance
157157
public static async Task<bool> IsNotTransientSlowAsync(string entityName, object entity, ISessionImplementor session, CancellationToken cancellationToken)
158158
{
159159
cancellationToken.ThrowIfCancellationRequested();
160-
if (entity.IsProxy())
161-
return true;
162-
if (session.PersistenceContext.IsEntryFor(entity))
163-
return true;
164160
return !await (IsTransientSlowAsync(entityName, entity, session, cancellationToken)).ConfigureAwait(false);
165161
}
166162

@@ -171,38 +167,25 @@ public static async Task<bool> IsNotTransientSlowAsync(string entityName, object
171167
/// <remarks>
172168
/// Don't hit the database to make the determination, instead return null;
173169
/// </remarks>
174-
public static Task<bool?> IsTransientFastAsync(string entityName, object entity, ISessionImplementor session, CancellationToken cancellationToken)
170+
public static async Task<bool?> IsTransientFastAsync(string entityName, object entity, ISessionImplementor session, CancellationToken cancellationToken)
175171
{
176-
if (cancellationToken.IsCancellationRequested)
172+
cancellationToken.ThrowIfCancellationRequested();
173+
if (Equals(Intercept.LazyPropertyInitializer.UnfetchedProperty, entity))
177174
{
178-
return Task.FromCanceled<bool?>(cancellationToken);
175+
// an unfetched association can only point to
176+
// an entity that already exists in the db
177+
return false;
179178
}
180-
try
181-
{
182-
if (Equals(Intercept.LazyPropertyInitializer.UnfetchedProperty, entity))
183-
{
184-
// an unfetched association can only point to
185-
// an entity that already exists in the db
186-
return Task.FromResult<bool?>(false);
187-
}
188179

189-
// let the interceptor inspect the instance to decide
190-
var isTransient = session.Interceptor.IsTransient(entity);
191-
if (isTransient.HasValue)
192-
return Task.FromResult<bool?>(isTransient);
180+
if (entity.IsProxy())
181+
return false;
193182

194-
if (entity is INHibernateProxy proxy && proxy.HibernateLazyInitializer.IsUninitialized)
195-
{
196-
return Task.FromResult<bool?>(false);
197-
}
183+
if (session.PersistenceContext.IsEntryFor(entity))
184+
return false;
198185

199-
// let the persister inspect the instance to decide
200-
return session.GetEntityPersister(entityName, entity).IsTransientAsync(entity, session, cancellationToken);
201-
}
202-
catch (System.Exception ex)
203-
{
204-
return Task.FromException<bool?>(ex);
205-
}
186+
// let the persister inspect the instance to decide
187+
return session.Interceptor.IsTransient(entity) ??
188+
await (session.GetEntityPersister(entityName, entity).IsTransientAsync(entity, session, cancellationToken)).ConfigureAwait(false);
206189
}
207190

208191
/// <summary>

src/NHibernate/Engine/ForeignKeys.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,6 @@ private bool IsNullifiable(string entityName, object obj)
155155
/// </remarks>
156156
public static bool IsNotTransientSlow(string entityName, object entity, ISessionImplementor session)
157157
{
158-
if (entity.IsProxy())
159-
return true;
160-
if (session.PersistenceContext.IsEntryFor(entity))
161-
return true;
162158
return !IsTransientSlow(entityName, entity, session);
163159
}
164160

@@ -171,25 +167,23 @@ public static bool IsNotTransientSlow(string entityName, object entity, ISession
171167
/// </remarks>
172168
public static bool? IsTransientFast(string entityName, object entity, ISessionImplementor session)
173169
{
170+
if (entity.IsProxy())
171+
return false;
172+
174173
if (Equals(Intercept.LazyPropertyInitializer.UnfetchedProperty, entity))
175174
{
176175
// an unfetched association can only point to
177176
// an entity that already exists in the db
178177
return false;
179178
}
180179

181-
// let the interceptor inspect the instance to decide
182-
var isTransient = session.Interceptor.IsTransient(entity);
183-
if (isTransient.HasValue)
184-
return isTransient;
185-
186-
if (entity is INHibernateProxy proxy && proxy.HibernateLazyInitializer.IsUninitialized)
187-
{
180+
if (session.PersistenceContext.IsEntryFor(entity))
188181
return false;
189-
}
190182

183+
// let the interceptor inspect the instance to decide
191184
// let the persister inspect the instance to decide
192-
return session.GetEntityPersister(entityName, entity).IsTransient(entity, session);
185+
return session.Interceptor.IsTransient(entity) ??
186+
session.GetEntityPersister(entityName, entity).IsTransient(entity, session);
193187
}
194188

195189
/// <summary>

src/NHibernate/Tuple/Entity/AbstractEntityTuplizer.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ public object Instantiate(object id)
114114

115115
public object GetIdentifier(object entity)
116116
{
117-
if (entity is INHibernateProxy proxy)
118-
{
119-
return proxy.HibernateLazyInitializer.Identifier;
120-
}
121-
122117
object id;
123118
if (entityMetamodel.IdentifierProperty.IsEmbedded)
124119
{

0 commit comments

Comments
 (0)