Skip to content

Fix default types #1484

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 1 commit into from
Dec 13, 2017
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
38 changes: 29 additions & 9 deletions src/NHibernate.Test/NHibernateUtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,56 @@ public class NHibernateUtilTest
[Test]
public void CanGuessTypeOfInt32ByValue()
{
Assert.AreEqual(NHibernateUtil.Int32, NHibernateUtil.GuessType(15));
Assert.That(NHibernateUtil.GuessType(15), Is.EqualTo(NHibernateUtil.Int32));
}

[Test]
public void CanGuessTypeOfInt32ByType()
{
Assert.AreEqual(NHibernateUtil.Int32,
NHibernateUtil.GuessType(typeof(int)));
Assert.That(NHibernateUtil.GuessType(typeof(int)), Is.EqualTo(NHibernateUtil.Int32));
}

[Test]
public void CanGuessTypeOfNullableInt32ByType()
{
Assert.AreEqual(NHibernateUtil.Int32,
NHibernateUtil.GuessType(typeof(int?)));
Assert.That(NHibernateUtil.GuessType(typeof(int?)), Is.EqualTo(NHibernateUtil.Int32));
}

[Test]
public void CanGuessTypeOfNullableInt32ByValue()
{
int? val = 15;
Assert.AreEqual(NHibernateUtil.Int32,
NHibernateUtil.GuessType(val));
Assert.That(NHibernateUtil.GuessType(val), Is.EqualTo(NHibernateUtil.Int32));
}

[Test]
public void CanGuessTypeOfDateTime()
{
Assert.AreEqual(NHibernateUtil.DateTime,
NHibernateUtil.GuessType(typeof(DateTime)));
Assert.That(NHibernateUtil.GuessType(typeof(DateTime)), Is.EqualTo(NHibernateUtil.DateTime));
}

[Test]
public void CanGuessTypeOfString()
{
Assert.That(NHibernateUtil.GuessType(typeof(string)), Is.EqualTo(NHibernateUtil.String));
}

[Test]
public void CanGuessTypeOfBoolean()
{
Assert.That(NHibernateUtil.GuessType(typeof(bool)), Is.EqualTo(NHibernateUtil.Boolean));
}

[Test]
public void CanGuessTypeOfDecimal()
{
Assert.That(NHibernateUtil.GuessType(typeof(decimal)), Is.EqualTo(NHibernateUtil.Decimal));
}

[Test]
public void CanGuessTypeOfTimeSpan()
{
Assert.That(NHibernateUtil.GuessType(typeof(TimeSpan)), Is.EqualTo(NHibernateUtil.TimeSpan));
}
}
}
9 changes: 3 additions & 6 deletions src/NHibernate/Async/NHibernateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@

namespace NHibernate
{
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using System.Threading;

public static partial class NHibernateUtil
{

Expand All @@ -47,13 +44,13 @@ public static partial class NHibernateUtil
{
return Task.CompletedTask;
}
else if (proxy.IsProxy())
if (proxy.IsProxy())
{
return ((INHibernateProxy)proxy).HibernateLazyInitializer.InitializeAsync(cancellationToken);
}
else if (proxy is IPersistentCollection)
else if (proxy is IPersistentCollection coll)
{
return ((IPersistentCollection)proxy).ForceInitializationAsync(cancellationToken);
return coll.ForceInitializationAsync(cancellationToken);
}
return Task.CompletedTask;
}
Expand Down
39 changes: 8 additions & 31 deletions src/NHibernate/NHibernateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

namespace NHibernate
{
using System.Collections.Generic;
using System.Reflection;

/// <summary>
/// Provides access to the full range of NHibernate built-in types.
/// IType instances may be used to bind values to query parameters.
Expand All @@ -21,26 +18,6 @@ namespace NHibernate
/// </summary>
public static partial class NHibernateUtil
{
static private readonly Dictionary<System.Type, IType> clrTypeToNHibernateType = new Dictionary<System.Type, IType>();

static NHibernateUtil()
{
FieldInfo[] fields = typeof(NHibernateUtil).GetFields();
foreach (FieldInfo info in fields)
{
if (typeof(IType).IsAssignableFrom(info.FieldType) == false)
continue;
IType type = (IType)info.GetValue(null);
clrTypeToNHibernateType[type.ReturnedClass] = type;
}

// There are multiple possibilites for boolean, strings and datetime.
// Override so that we use the most natural mapping.
clrTypeToNHibernateType[Boolean.ReturnedClass] = Boolean;
clrTypeToNHibernateType[String.ReturnedClass] = String;
clrTypeToNHibernateType[DateTime.ReturnedClass] = DateTime;
}

/// <summary>
/// Guesses the IType of this object
/// </summary>
Expand All @@ -61,8 +38,8 @@ public static IType GuessType(System.Type type)
{
type = type.UnwrapIfNullable();

IType value;
if (clrTypeToNHibernateType.TryGetValue(type, out value))
var value = TypeFactory.GetDefaultTypeFor(type);
if (value != null)
return value;

if (type.IsEnum)
Expand All @@ -73,7 +50,7 @@ public static IType GuessType(System.Type type)
{
return Custom(type);
}

return Entity(type);
}

Expand Down Expand Up @@ -401,13 +378,13 @@ public static void Initialize(object proxy)
{
return;
}
else if (proxy.IsProxy())
if (proxy.IsProxy())
{
((INHibernateProxy)proxy).HibernateLazyInitializer.Initialize();
}
else if (proxy is IPersistentCollection)
else if (proxy is IPersistentCollection coll)
{
((IPersistentCollection)proxy).ForceInitialization();
coll.ForceInitialization();
}
}

Expand Down Expand Up @@ -533,7 +510,7 @@ public static void Close(IEnumerator enumerator)
EnumerableImpl hibernateEnumerator = enumerator as EnumerableImpl;
if (hibernateEnumerator == null)
{
throw new ArgumentException("Not a NHibernate enumerator", "enumerator");
throw new ArgumentException("Not a NHibernate enumerator", nameof(enumerator));
}
hibernateEnumerator.Dispose();
}
Expand All @@ -547,7 +524,7 @@ public static void Close(IEnumerable enumerable)
EnumerableImpl hibernateEnumerable = enumerable as EnumerableImpl;
if (hibernateEnumerable == null)
{
throw new ArgumentException("Not a NHibernate enumerable", "enumerable");
throw new ArgumentException("Not a NHibernate enumerable", nameof(enumerable));
}
hibernateEnumerable.Dispose();
}
Expand Down
9 changes: 9 additions & 0 deletions src/NHibernate/Type/TypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,15 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
return GetSerializableType(typeClass);
}

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

[MethodImpl(MethodImplOptions.Synchronized)]
public static NullableType GetAnsiStringType(int length)
Expand Down