Skip to content

Commit f334272

Browse files
committed
Refactor ForeignKeys.IsTransient to reduce Async method generations
1 parent ae53540 commit f334272

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

src/NHibernate.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARGUMENTS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
1515
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARRAY_INITIALIZER_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
1616
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_BEFORE_FIRST_TYPE_PARAMETER_CONSTRAINT/@EntryValue">True</s:Boolean>
17+
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">400</s:Int64>
1718
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_PARAMETERS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
1819
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">False</s:Boolean>
1920
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>

src/NHibernate/Engine/ForeignKeys.cs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ public static bool IsNotTransient(string entityName, System.Object entity, bool?
164164
return !IsTransient(entityName, entity, assumed, session);
165165
}
166166

167+
168+
/// <summary>
169+
/// Is this instance persistent or detached?
170+
/// </summary>
171+
internal static bool? IsNotTransient(string entityName, object entity, ISessionImplementor session)
172+
{
173+
if (entity.IsProxy())
174+
return true;
175+
if (session.PersistenceContext.IsEntryFor(entity))
176+
return true;
177+
return !IsTransient(entityName, entity, session);
178+
}
179+
167180
/// <summary>
168181
/// Is this instance, which we know is not persistent, actually transient?
169182
/// If <tt>assumed</tt> is non-null, don't hit the database to make the
@@ -176,6 +189,24 @@ public static bool IsNotTransient(string entityName, System.Object entity, bool?
176189
/// prepared to "recover" in the case that this assumed result is incorrect.
177190
/// </remarks>
178191
public static bool IsTransient(string entityName, object entity, bool? assumed, ISessionImplementor session)
192+
{
193+
var isUnsaved = IsTransient(entityName, entity, session);
194+
if (isUnsaved.HasValue)
195+
return isUnsaved.Value;
196+
if (assumed.HasValue)
197+
// we use the assumed value, if there is one, to avoid hitting
198+
// the database
199+
return assumed.Value;
200+
return IsPersisted(entityName, entity, session);
201+
}
202+
203+
/// <summary>
204+
/// Is this instance, which we know is not persistent, actually transient?
205+
/// If <tt>assumed</tt> is non-null, don't hit the database to make the
206+
/// determination, instead assume that value; the client code must be
207+
/// prepared to "recover" in the case that this assumed result is incorrect.
208+
/// </summary>
209+
public static bool? IsTransient(string entityName, object entity, ISessionImplementor session)
179210
{
180211
if (Equals(Intercept.LazyPropertyInitializer.UnfetchedProperty, entity))
181212
{
@@ -190,16 +221,13 @@ public static bool IsTransient(string entityName, object entity, bool? assumed,
190221
return isUnsaved.Value;
191222

192223
// let the persister inspect the instance to decide
193-
IEntityPersister persister = session.GetEntityPersister(entityName, entity);
194-
isUnsaved = persister.IsTransient(entity, session);
195-
if (isUnsaved.HasValue)
196-
return isUnsaved.Value;
197-
198-
// we use the assumed value, if there is one, to avoid hitting
199-
// the database
200-
if (assumed.HasValue)
201-
return assumed.Value;
224+
var persister = session.GetEntityPersister(entityName, entity);
225+
return persister.IsTransient(entity, session);
226+
}
202227

228+
public static bool IsPersisted(string entityName, object entity, ISessionImplementor session)
229+
{
230+
var persister = session.GetEntityPersister(entityName, entity);
203231
if (persister.IdentifierGenerator is Assigned)
204232
{
205233
// When using assigned identifiers we cannot tell if an entity
@@ -247,7 +275,7 @@ public static object GetEntityIdentifierIfNotUnsaved(string entityName, object e
247275
return entity;
248276
/**********************************************/
249277

250-
if (IsTransient(entityName, entity, false, session))
278+
if (IsTransient(entityName, entity, session) ?? false)
251279
{
252280
/***********************************************/
253281
// TODO NH verify the behavior of NH607 test

src/NHibernate/Event/Default/DefaultLockEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public virtual void OnLock(LockEvent @event)
4242
{
4343
IEntityPersister persister = source.GetEntityPersister(@event.EntityName, entity);
4444
object id = persister.GetIdentifier(entity);
45-
if (!ForeignKeys.IsNotTransient(@event.EntityName, entity, false, source))
45+
if (ForeignKeys.IsNotTransient(@event.EntityName, entity, source) == true)
4646
{
4747
throw new TransientObjectException("cannot lock an unsaved transient instance: " + persister.EntityName);
4848
}

src/NHibernate/Event/Default/DefaultRefreshEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public virtual void OnRefresh(RefreshEvent @event, IDictionary refreshedAlready)
118118

119119
// NH Different behavior : we are ignoring transient entities without throw any kind of exception
120120
// because a transient entity is "self refreshed"
121-
if (!ForeignKeys.IsTransient(persister.EntityName, obj, result == null, @event.Session))
121+
if (!(ForeignKeys.IsTransient(persister.EntityName, obj, @event.Session) ?? (result == null)))
122122
UnresolvableObjectException.ThrowIfNull(result, id, persister.EntityName);
123123
}
124124

src/NHibernate/Type/EntityType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public override object Replace(object original, object target, ISessionImplement
239239
{
240240
return target;
241241
}
242-
if (session.GetContextEntityIdentifier(original) == null && ForeignKeys.IsTransient(associatedEntityName, original, false, session))
242+
if (session.GetContextEntityIdentifier(original) == null && (ForeignKeys.IsTransient(associatedEntityName, original, session) ?? false))
243243
{
244244
object copy = session.Factory.GetEntityPersister(associatedEntityName).Instantiate(null);
245245
//TODO: should this be Session.instantiate(Persister, ...)?

0 commit comments

Comments
 (0)