Skip to content

Commit 57d7e3b

Browse files
committed
Make use of C#7.2 readonly struct and in modifier to keep assigment by ref in methods
1 parent 4372165 commit 57d7e3b

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed

src/NHibernate.DomainModel/NHibernate.DomainModel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFrameworks>$(NhLibTargetFrameworks)</TargetFrameworks>
66
<IsTestProject>true</IsTestProject>
77
<NoWarn>$(NoWarn);3001;3002;3003;3005</NoWarn>
8+
<LangVersion>7.2</LangVersion>
89
</PropertyGroup>
910
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">
1011
<DefineConstants>NETFX;$(DefineConstants)</DefineConstants>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<IsTestProject>true</IsTestProject>
77
<NoWarn>$(NoWarn);3001;3002;3003;3005</NoWarn>
88
<ContentSQLiteInteropFiles>true</ContentSQLiteInteropFiles>
9+
<LangVersion>7.2</LangVersion>
910
</PropertyGroup>
1011
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">
1112
<DefineConstants>NETFX;$(DefineConstants)</DefineConstants>

src/NHibernate.TestDatabaseSetup/NHibernate.TestDatabaseSetup.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<TargetFrameworks>$(NhAppTargetFrameworks)</TargetFrameworks>
77
<IsTestProject>true</IsTestProject>
88
<NoWarn>$(NoWarn);3001;3002;3003;3005</NoWarn>
9+
<LangVersion>7.2</LangVersion>
910
</PropertyGroup>
1011
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">
1112
<DefineConstants>NETFX;$(DefineConstants)</DefineConstants>

src/NHibernate/Engine/EntityKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace NHibernate.Engine
1111
/// and the identifier space (eg. tablename)
1212
/// </summary>
1313
[Serializable]
14-
public struct EntityKey : ISerializable, IEquatable<EntityKey>
14+
public readonly struct EntityKey : ISerializable, IEquatable<EntityKey>
1515
{
1616
public static EntityKey Null { get; } = new EntityKey();
1717

src/NHibernate/Engine/IPersistenceContext.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public partial interface IPersistenceContext
110110
/// <item><description>an entry of NO_ROW here is interpreted as an exception</description></item>
111111
/// </list>
112112
/// </remarks>
113-
object[] GetCachedDatabaseSnapshot(EntityKey key);
113+
object[] GetCachedDatabaseSnapshot(in EntityKey key);
114114

115115
/// <summary>
116116
/// Get the values of the natural id fields as known to the underlying
@@ -120,22 +120,22 @@ public partial interface IPersistenceContext
120120
object[] GetNaturalIdSnapshot(object id, IEntityPersister persister);
121121

122122
/// <summary> Add a canonical mapping from entity key to entity instance</summary>
123-
void AddEntity(EntityKey key, object entity);
123+
void AddEntity(in EntityKey key, object entity);
124124

125125
/// <summary>
126126
/// Get the entity instance associated with the given <tt>EntityKey</tt>
127127
/// </summary>
128-
object GetEntity(EntityKey key);
128+
object GetEntity(in EntityKey key);
129129

130130
/// <summary> Is there an entity with the given key in the persistence context</summary>
131-
bool ContainsEntity(EntityKey key);
131+
bool ContainsEntity(in EntityKey key);
132132

133133
/// <summary>
134134
/// Remove an entity from the session cache, also clear
135135
/// up other state associated with the entity, all except
136136
/// for the <tt>EntityEntry</tt>
137137
/// </summary>
138-
object RemoveEntity(EntityKey key);
138+
object RemoveEntity(in EntityKey key);
139139

140140
/// <summary> Get an entity cached by unique key</summary>
141141
object GetEntity(EntityUniqueKey euk);
@@ -160,7 +160,7 @@ public partial interface IPersistenceContext
160160
CollectionEntry GetCollectionEntry(IPersistentCollection coll);
161161

162162
/// <summary> Adds an entity to the internal caches.</summary>
163-
EntityEntry AddEntity(object entity, Status status, object[] loadedState, EntityKey entityKey, object version,
163+
EntityEntry AddEntity(object entity, Status status, object[] loadedState, in EntityKey entityKey, object version,
164164
LockMode lockMode, bool existsInDatabase, IEntityPersister persister,
165165
bool disableVersionIncrement, bool lazyPropertiesAreUnfetched);
166166

@@ -209,9 +209,9 @@ EntityEntry AddEntry(object entity, Status status, object[] loadedState, object
209209
/// Attempts to check whether the given key represents an entity already loaded within the
210210
/// current session.
211211
/// </summary>
212-
/// <param name="obj">The entity reference against which to perform the uniqueness check.</param>
213212
/// <param name="key">The entity key.</param>
214-
void CheckUniqueness(EntityKey key, object obj);
213+
/// <param name="obj">The entity reference against which to perform the uniqueness check.</param>
214+
void CheckUniqueness(in EntityKey key, object obj);
215215

216216
/// <summary>
217217
/// If the existing proxy is insufficiently "narrow" (derived), instantiate a new proxy
@@ -224,14 +224,14 @@ EntityEntry AddEntry(object entity, Status status, object[] loadedState, object
224224
/// <param name="key">The internal cache key for the proxied entity. </param>
225225
/// <param name="obj">(optional) the actual proxied entity instance. </param>
226226
/// <returns> An appropriately narrowed instance. </returns>
227-
object NarrowProxy(INHibernateProxy proxy, IEntityPersister persister, EntityKey key, object obj);
227+
object NarrowProxy(INHibernateProxy proxy, IEntityPersister persister, in EntityKey key, object obj);
228228

229229
/// <summary>
230230
/// Return the existing proxy associated with the given <tt>EntityKey</tt>, or the
231231
/// third argument (the entity associated with the key) if no proxy exists. Init
232232
/// the proxy to the target implementation, if necessary.
233233
/// </summary>
234-
object ProxyFor(IEntityPersister persister, EntityKey key, object impl);
234+
object ProxyFor(IEntityPersister persister,in EntityKey key, object impl);
235235

236236
/// <summary>
237237
/// Return the existing proxy associated with the given <tt>EntityKey</tt>, or the
@@ -319,13 +319,13 @@ EntityEntry AddEntry(object entity, Status status, object[] loadedState, object
319319
CollectionEntry GetCollectionEntryOrNull(object collection);
320320

321321
/// <summary> Get an existing proxy by key</summary>
322-
object GetProxy(EntityKey key);
322+
object GetProxy(in EntityKey key);
323323

324324
/// <summary> Add a proxy to the session cache</summary>
325-
void AddProxy(EntityKey key, INHibernateProxy proxy);
325+
void AddProxy(in EntityKey key, INHibernateProxy proxy);
326326

327327
/// <summary> Remove a proxy from the session cache</summary>
328-
object RemoveProxy(EntityKey key);
328+
object RemoveProxy(in EntityKey key);
329329

330330
/// <summary> Called before cascading</summary>
331331
int IncrementCascadeLevel();
@@ -353,10 +353,10 @@ EntityEntry AddEntry(object entity, Status status, object[] loadedState, object
353353
/// <summary>
354354
/// Record the fact that the association belonging to the keyed entity is null.
355355
/// </summary>
356-
void AddNullProperty(EntityKey ownerKey, string propertyName);
356+
void AddNullProperty(in EntityKey ownerKey, string propertyName);
357357

358358
/// <summary> Is the association property belonging to the keyed entity null?</summary>
359-
bool IsPropertyNull(EntityKey ownerKey, string propertyName);
359+
bool IsPropertyNull(in EntityKey ownerKey, string propertyName);
360360

361361
/// <summary>
362362
/// Change the read-only status of an entity (or proxy).
@@ -391,7 +391,7 @@ EntityEntry AddEntry(object entity, Status status, object[] loadedState, object
391391
/// <seealso cref="IPersistenceContext.SetReadOnly(object, bool)" />
392392
bool IsReadOnly(object entityOrProxy);
393393

394-
void ReplaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, object generatedId);
394+
void ReplaceDelayedEntityIdentityInsertKeys(in EntityKey oldKey, object generatedId);
395395

396396
/// <summary>Is in a two-phase load? </summary>
397397
bool IsLoadFinished { get; }

src/NHibernate/Engine/StatefulPersistenceContext.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public object[] GetDatabaseSnapshot(object id, IEntityPersister persister)
355355
/// <item><description>an entry of NO_ROW here is interpreted as an exception</description></item>
356356
/// </list>
357357
/// </remarks>
358-
public object[] GetCachedDatabaseSnapshot(EntityKey key)
358+
public object[] GetCachedDatabaseSnapshot(in EntityKey key)
359359
{
360360
object snapshot;
361361
if (!entitySnapshotsByKey.TryGetValue(key, out snapshot))
@@ -418,7 +418,7 @@ public object[] GetNaturalIdSnapshot(object id, IEntityPersister persister)
418418
}
419419

420420
/// <summary> Add a canonical mapping from entity key to entity instance</summary>
421-
public void AddEntity(EntityKey key, object entity)
421+
public void AddEntity(in EntityKey key, object entity)
422422
{
423423
entitiesByKey[key] = entity;
424424
BatchFetchQueue.RemoveBatchLoadableEntityKey(key);
@@ -427,15 +427,15 @@ public void AddEntity(EntityKey key, object entity)
427427
/// <summary>
428428
/// Get the entity instance associated with the given <tt>EntityKey</tt>
429429
/// </summary>
430-
public object GetEntity(EntityKey key)
430+
public object GetEntity(in EntityKey key)
431431
{
432432
object result;
433433
entitiesByKey.TryGetValue(key, out result);
434434
return result;
435435
}
436436

437437
/// <summary> Is there an entity with the given key in the persistence context</summary>
438-
public bool ContainsEntity(EntityKey key)
438+
public bool ContainsEntity(in EntityKey key)
439439
{
440440
return entitiesByKey.ContainsKey(key);
441441
}
@@ -445,7 +445,7 @@ public bool ContainsEntity(EntityKey key)
445445
/// up other state associated with the entity, all except
446446
/// for the <tt>EntityEntry</tt>
447447
/// </summary>
448-
public object RemoveEntity(EntityKey key)
448+
public object RemoveEntity(in EntityKey key)
449449
{
450450
object tempObject = entitiesByKey[key];
451451
entitiesByKey.Remove(key);
@@ -513,9 +513,10 @@ public CollectionEntry GetCollectionEntry(IPersistentCollection coll)
513513
}
514514

515515
/// <summary> Adds an entity to the internal caches.</summary>
516-
public EntityEntry AddEntity(object entity, Status status, object[] loadedState, EntityKey entityKey, object version,
517-
LockMode lockMode, bool existsInDatabase, IEntityPersister persister,
518-
bool disableVersionIncrement, bool lazyPropertiesAreUnfetched)
516+
public EntityEntry AddEntity(
517+
object entity, Status status, object[] loadedState, in EntityKey entityKey, object version,
518+
LockMode lockMode, bool existsInDatabase, IEntityPersister persister,
519+
bool disableVersionIncrement, bool lazyPropertiesAreUnfetched)
519520
{
520521
AddEntity(entityKey, entity);
521522

@@ -681,9 +682,9 @@ public object UnproxyAndReassociate(object maybeProxy)
681682
/// Attempts to check whether the given key represents an entity already loaded within the
682683
/// current session.
683684
/// </summary>
684-
/// <param name="obj">The entity reference against which to perform the uniqueness check.</param>
685685
/// <param name="key">The entity key.</param>
686-
public void CheckUniqueness(EntityKey key, object obj)
686+
/// <param name="obj">The entity reference against which to perform the uniqueness check.</param>
687+
public void CheckUniqueness(in EntityKey key, object obj)
687688
{
688689
object entity = GetEntity(key);
689690
if (entity == obj)
@@ -707,7 +708,7 @@ public void CheckUniqueness(EntityKey key, object obj)
707708
/// <param name="key">The internal cache key for the proxied entity. </param>
708709
/// <param name="obj">(optional) the actual proxied entity instance. </param>
709710
/// <returns> An appropriately narrowed instance. </returns>
710-
public object NarrowProxy(INHibernateProxy proxy, IEntityPersister persister, EntityKey key, object obj)
711+
public object NarrowProxy(INHibernateProxy proxy, IEntityPersister persister, in EntityKey key, object obj)
711712
{
712713
bool alreadyNarrow = persister.ConcreteProxyClass.IsInstanceOfType(proxy);
713714

@@ -751,7 +752,7 @@ public object NarrowProxy(INHibernateProxy proxy, IEntityPersister persister, En
751752
/// third argument (the entity associated with the key) if no proxy exists. Init
752753
/// the proxy to the target implementation, if necessary.
753754
/// </summary>
754-
public object ProxyFor(IEntityPersister persister, EntityKey key, object impl)
755+
public object ProxyFor(IEntityPersister persister, in EntityKey key, object impl)
755756
{
756757
if (!persister.HasProxy || key.IsNull)
757758
return impl;
@@ -1045,7 +1046,7 @@ public CollectionEntry GetCollectionEntryOrNull(object collection)
10451046
}
10461047

10471048
/// <summary> Get an existing proxy by key</summary>
1048-
public object GetProxy(EntityKey key)
1049+
public object GetProxy(in EntityKey key)
10491050
{
10501051
INHibernateProxy result;
10511052
if (proxiesByKey.TryGetValue(key, out result))
@@ -1055,13 +1056,13 @@ public object GetProxy(EntityKey key)
10551056
}
10561057

10571058
/// <summary> Add a proxy to the session cache</summary>
1058-
public void AddProxy(EntityKey key, INHibernateProxy proxy)
1059+
public void AddProxy(in EntityKey key, INHibernateProxy proxy)
10591060
{
10601061
proxiesByKey[key] = proxy;
10611062
}
10621063

10631064
/// <summary> Remove a proxy from the session cache</summary>
1064-
public object RemoveProxy(EntityKey key)
1065+
public object RemoveProxy(in EntityKey key)
10651066
{
10661067
if (batchFetchQueue != null)
10671068
{
@@ -1264,13 +1265,13 @@ private object GetIndexInParent(string property, object childEntity, IEntityPers
12641265
/// <summary>
12651266
/// Record the fact that the association belonging to the keyed entity is null.
12661267
/// </summary>
1267-
public void AddNullProperty(EntityKey ownerKey, string propertyName)
1268+
public void AddNullProperty(in EntityKey ownerKey, string propertyName)
12681269
{
12691270
nullAssociations.Add(new AssociationKey(ownerKey, propertyName));
12701271
}
12711272

12721273
/// <summary> Is the association property belonging to the keyed entity null?</summary>
1273-
public bool IsPropertyNull(EntityKey ownerKey, string propertyName)
1274+
public bool IsPropertyNull(in EntityKey ownerKey, string propertyName)
12741275
{
12751276
return nullAssociations.Contains(new AssociationKey(ownerKey, propertyName));
12761277
}
@@ -1351,7 +1352,7 @@ public bool IsReadOnly(object entityOrProxy)
13511352
return isReadOnly;
13521353
}
13531354

1354-
public void ReplaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, object generatedId)
1355+
public void ReplaceDelayedEntityIdentityInsertKeys(in EntityKey oldKey, object generatedId)
13551356
{
13561357
object tempObject = entitiesByKey[oldKey];
13571358
entitiesByKey.Remove(oldKey);

src/NHibernate/NHibernate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<PackageDescription>NHibernate is a mature, open source object-relational mapper for the .NET framework. It is actively developed, fully featured and used in thousands of successful projects.</PackageDescription>
1414
<PackageTags>ORM; O/RM; DataBase; DAL; ObjectRelationalMapping; NHibernate; ADO.Net; Core</PackageTags>
15+
<LangVersion>7.3</LangVersion>
1516
</PropertyGroup>
1617

1718
<PropertyGroup Condition="'$(TargetFramework)' == 'net461'">

0 commit comments

Comments
 (0)