Skip to content

Commit b11168a

Browse files
NH-3884 - Preliminary clean-up of NHibernateUtil
* Removing logic duplication for getting default NHibernate type for a clr type.
1 parent 1bdcb54 commit b11168a

File tree

3 files changed

+20
-36
lines changed

3 files changed

+20
-36
lines changed

src/NHibernate/Async/NHibernateUtil.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020

2121
namespace NHibernate
2222
{
23-
using System.Collections.Generic;
24-
using System.Reflection;
2523
using System.Threading.Tasks;
2624
using System.Threading;
27-
2825
public static partial class NHibernateUtil
2926
{
3027

@@ -47,13 +44,13 @@ public static partial class NHibernateUtil
4744
{
4845
return Task.CompletedTask;
4946
}
50-
else if (proxy.IsProxy())
47+
if (proxy.IsProxy())
5148
{
5249
return ((INHibernateProxy)proxy).HibernateLazyInitializer.InitializeAsync(cancellationToken);
5350
}
54-
else if (proxy is IPersistentCollection)
51+
else if (proxy is IPersistentCollection coll)
5552
{
56-
return ((IPersistentCollection)proxy).ForceInitializationAsync(cancellationToken);
53+
return coll.ForceInitializationAsync(cancellationToken);
5754
}
5855
return Task.CompletedTask;
5956
}

src/NHibernate/NHibernateUtil.cs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,13 @@
1010

1111
namespace NHibernate
1212
{
13-
using System.Collections.Generic;
14-
using System.Reflection;
15-
1613
/// <summary>
1714
/// Provides access to the full range of NHibernate built-in types.
1815
/// IType instances may be used to bind values to query parameters.
1916
/// Also a factory for new Blobs and Clobs.
2017
/// </summary>
2118
public static partial class NHibernateUtil
2219
{
23-
static private readonly Dictionary<System.Type, IType> clrTypeToNHibernateType = new Dictionary<System.Type, IType>();
24-
25-
static NHibernateUtil()
26-
{
27-
FieldInfo[] fields = typeof(NHibernateUtil).GetFields();
28-
foreach (FieldInfo info in fields)
29-
{
30-
if (typeof(IType).IsAssignableFrom(info.FieldType) == false)
31-
continue;
32-
IType type = (IType)info.GetValue(null);
33-
clrTypeToNHibernateType[type.ReturnedClass] = type;
34-
}
35-
36-
// There are multiple possibilites for boolean and strings.
37-
// Override so that we use the most natural mapping.
38-
clrTypeToNHibernateType[Boolean.ReturnedClass] = Boolean;
39-
clrTypeToNHibernateType[String.ReturnedClass] = String;
40-
}
41-
4220
/// <summary>
4321
/// Guesses the IType of this object
4422
/// </summary>
@@ -59,8 +37,8 @@ public static IType GuessType(System.Type type)
5937
{
6038
type = type.UnwrapIfNullable();
6139

62-
IType value;
63-
if (clrTypeToNHibernateType.TryGetValue(type, out value))
40+
var value = TypeFactory.GetDefaultTypeFor(type);
41+
if (value != null)
6442
return value;
6543

6644
if (type.IsEnum)
@@ -71,7 +49,7 @@ public static IType GuessType(System.Type type)
7149
{
7250
return Custom(type);
7351
}
74-
52+
7553
return Entity(type);
7654
}
7755

@@ -381,13 +359,13 @@ public static void Initialize(object proxy)
381359
{
382360
return;
383361
}
384-
else if (proxy.IsProxy())
362+
if (proxy.IsProxy())
385363
{
386364
((INHibernateProxy)proxy).HibernateLazyInitializer.Initialize();
387365
}
388-
else if (proxy is IPersistentCollection)
366+
else if (proxy is IPersistentCollection coll)
389367
{
390-
((IPersistentCollection)proxy).ForceInitialization();
368+
coll.ForceInitialization();
391369
}
392370
}
393371

@@ -513,7 +491,7 @@ public static void Close(IEnumerator enumerator)
513491
EnumerableImpl hibernateEnumerator = enumerator as EnumerableImpl;
514492
if (hibernateEnumerator == null)
515493
{
516-
throw new ArgumentException("Not a NHibernate enumerator", "enumerator");
494+
throw new ArgumentException("Not a NHibernate enumerator", nameof(enumerator));
517495
}
518496
hibernateEnumerator.Dispose();
519497
}
@@ -527,7 +505,7 @@ public static void Close(IEnumerable enumerable)
527505
EnumerableImpl hibernateEnumerable = enumerable as EnumerableImpl;
528506
if (hibernateEnumerable == null)
529507
{
530-
throw new ArgumentException("Not a NHibernate enumerable", "enumerable");
508+
throw new ArgumentException("Not a NHibernate enumerable", nameof(enumerable));
531509
}
532510
hibernateEnumerable.Dispose();
533511
}

src/NHibernate/Type/TypeFactory.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,15 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
535535
return GetSerializableType(typeClass);
536536
}
537537

538+
/// <summary>
539+
/// Get the current default NHibernate type for a .Net type.
540+
/// </summary>
541+
/// <param name="type">The .Net type for which to get the corresponding default NHibernate type.</param>
542+
/// <returns>The current default NHibernate type for a .Net type if any, otherwise <see langword="null" />.</returns>
543+
public static IType GetDefaultTypeFor(System.Type type)
544+
{
545+
return typeByTypeOfName.TryGetValue(type.FullName, out var nhType) ? nhType : null;
546+
}
538547

539548
public static NullableType GetAnsiStringType(int length)
540549
{

0 commit comments

Comments
 (0)