Skip to content

Commit 9827d90

Browse files
authored
Merge branch 'master' into defAliases
2 parents 0c81cd7 + 6d8d5d9 commit 9827d90

File tree

9 files changed

+89
-52
lines changed

9 files changed

+89
-52
lines changed

src/NHibernate.Test/NHibernateUtilTest.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,56 @@ public class NHibernateUtilTest
1010
[Test]
1111
public void CanGuessTypeOfInt32ByValue()
1212
{
13-
Assert.AreEqual(NHibernateUtil.Int32, NHibernateUtil.GuessType(15));
13+
Assert.That(NHibernateUtil.GuessType(15), Is.EqualTo(NHibernateUtil.Int32));
1414
}
1515

1616
[Test]
1717
public void CanGuessTypeOfInt32ByType()
1818
{
19-
Assert.AreEqual(NHibernateUtil.Int32,
20-
NHibernateUtil.GuessType(typeof(int)));
19+
Assert.That(NHibernateUtil.GuessType(typeof(int)), Is.EqualTo(NHibernateUtil.Int32));
2120
}
2221

2322
[Test]
2423
public void CanGuessTypeOfNullableInt32ByType()
2524
{
26-
Assert.AreEqual(NHibernateUtil.Int32,
27-
NHibernateUtil.GuessType(typeof(int?)));
25+
Assert.That(NHibernateUtil.GuessType(typeof(int?)), Is.EqualTo(NHibernateUtil.Int32));
2826
}
2927

3028
[Test]
3129
public void CanGuessTypeOfNullableInt32ByValue()
3230
{
3331
int? val = 15;
34-
Assert.AreEqual(NHibernateUtil.Int32,
35-
NHibernateUtil.GuessType(val));
32+
Assert.That(NHibernateUtil.GuessType(val), Is.EqualTo(NHibernateUtil.Int32));
3633
}
3734

3835
[Test]
3936
public void CanGuessTypeOfDateTime()
4037
{
41-
Assert.AreEqual(NHibernateUtil.DateTime,
42-
NHibernateUtil.GuessType(typeof(DateTime)));
38+
Assert.That(NHibernateUtil.GuessType(typeof(DateTime)), Is.EqualTo(NHibernateUtil.DateTime));
39+
}
40+
41+
[Test]
42+
public void CanGuessTypeOfString()
43+
{
44+
Assert.That(NHibernateUtil.GuessType(typeof(string)), Is.EqualTo(NHibernateUtil.String));
45+
}
46+
47+
[Test]
48+
public void CanGuessTypeOfBoolean()
49+
{
50+
Assert.That(NHibernateUtil.GuessType(typeof(bool)), Is.EqualTo(NHibernateUtil.Boolean));
51+
}
52+
53+
[Test]
54+
public void CanGuessTypeOfDecimal()
55+
{
56+
Assert.That(NHibernateUtil.GuessType(typeof(decimal)), Is.EqualTo(NHibernateUtil.Decimal));
57+
}
58+
59+
[Test]
60+
public void CanGuessTypeOfTimeSpan()
61+
{
62+
Assert.That(NHibernateUtil.GuessType(typeof(TimeSpan)), Is.EqualTo(NHibernateUtil.TimeSpan));
4363
}
4464
}
4565
}

src/NHibernate/Async/Impl/MultiCriteriaImpl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private async Task GetResultsFromDatabaseAsync(IList results, CancellationToken
151151

152152
try
153153
{
154-
using (var reader = await (resultSetsCommand.GetReaderAsync(null, cancellationToken)).ConfigureAwait(false))
154+
using (var reader = await (resultSetsCommand.GetReaderAsync(_timeout, cancellationToken)).ConfigureAwait(false))
155155
{
156156
var hydratedObjects = new List<object>[loaders.Count];
157157
List<EntityKey[]>[] subselectResultKeys = new List<EntityKey[]>[loaders.Count];

src/NHibernate/Async/Impl/MultiQueryImpl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected async Task<List<object>> DoListAsync(CancellationToken cancellationTok
8484

8585
try
8686
{
87-
using (var reader = await (resultSetsCommand.GetReaderAsync(commandTimeout != RowSelection.NoValue ? commandTimeout : (int?)null, cancellationToken)).ConfigureAwait(false))
87+
using (var reader = await (resultSetsCommand.GetReaderAsync(_timeout, cancellationToken)).ConfigureAwait(false))
8888
{
8989
if (log.IsDebugEnabled())
9090
{

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/IMultiCriteria.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.Collections;
23
using NHibernate.Criterion;
4+
using NHibernate.Impl;
35
using NHibernate.Transform;
46

57
namespace NHibernate
@@ -154,4 +156,26 @@ public partial interface IMultiCriteria
154156
/// <returns></returns>
155157
object GetResult(string key);
156158
}
159+
160+
public static class MultiCriteriaExtensions
161+
{
162+
//6.0 TODO: Convert to interface method
163+
/// <summary>
164+
/// Set a timeout for the underlying ADO.NET query
165+
/// </summary>
166+
public static IMultiCriteria SetTimeout(this IMultiCriteria multiCriteria, int timeout)
167+
{
168+
if (multiCriteria == null)
169+
{
170+
throw new ArgumentNullException(nameof(multiCriteria));
171+
}
172+
173+
if (multiCriteria is MultiCriteriaImpl impl)
174+
{
175+
return impl.SetTimeout(timeout);
176+
}
177+
178+
throw new NotSupportedException(multiCriteria.GetType() + " does not support SetTimeout");
179+
}
180+
}
157181
}

src/NHibernate/Impl/MultiCriteriaImpl.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public partial class MultiCriteriaImpl : IMultiCriteria
3535
private string cacheRegion;
3636
private IResultTransformer resultTransformer;
3737
private readonly IResultSetsCommand resultSetsCommand;
38+
private int? _timeout;
3839

3940
/// <summary>
4041
/// Initializes a new instance of the <see cref="MultiCriteriaImpl"/> class.
@@ -221,7 +222,7 @@ private void GetResultsFromDatabase(IList results)
221222

222223
try
223224
{
224-
using (var reader = resultSetsCommand.GetReader(null))
225+
using (var reader = resultSetsCommand.GetReader(_timeout))
225226
{
226227
var hydratedObjects = new List<object>[loaders.Count];
227228
List<EntityKey[]>[] subselectResultKeys = new List<EntityKey[]>[loaders.Count];
@@ -474,5 +475,14 @@ private void ThrowIfKeyAlreadyExists(string key)
474475
if (criteriaResultPositions.ContainsKey(key))
475476
throw new InvalidOperationException(String.Format("The key '{0}' already exists", key));
476477
}
478+
479+
/// <summary>
480+
/// Set a timeout for the underlying ADO.NET query
481+
/// </summary>
482+
public IMultiCriteria SetTimeout(int timeout)
483+
{
484+
_timeout = timeout == RowSelection.NoValue ? (int?) null : timeout;
485+
return this;
486+
}
477487
}
478488
}

src/NHibernate/Impl/MultiQueryImpl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public partial class MultiQueryImpl : IMultiQuery
2929
private IList queryResults;
3030
private readonly Dictionary<string, int> queryResultPositions = new Dictionary<string, int>();
3131
private string cacheRegion;
32-
private int commandTimeout = RowSelection.NoValue;
32+
private int? _timeout;
3333
private bool isCacheable;
3434
private readonly ISessionImplementor session;
3535
private IResultTransformer resultTransformer;
@@ -64,7 +64,7 @@ public IMultiQuery SetForceCacheRefresh(bool cacheRefresh)
6464

6565
public IMultiQuery SetTimeout(int timeout)
6666
{
67-
commandTimeout = timeout;
67+
_timeout = timeout == RowSelection.NoValue ? (int?) null : timeout;
6868
return this;
6969
}
7070

@@ -526,7 +526,7 @@ protected List<object> DoList()
526526

527527
try
528528
{
529-
using (var reader = resultSetsCommand.GetReader(commandTimeout != RowSelection.NoValue ? commandTimeout : (int?)null))
529+
using (var reader = resultSetsCommand.GetReader(_timeout))
530530
{
531531
if (log.IsDebugEnabled())
532532
{

src/NHibernate/NHibernateUtil.cs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
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.
@@ -21,26 +18,6 @@ namespace NHibernate
2118
/// </summary>
2219
public static partial class NHibernateUtil
2320
{
24-
static private readonly Dictionary<System.Type, IType> clrTypeToNHibernateType = new Dictionary<System.Type, IType>();
25-
26-
static NHibernateUtil()
27-
{
28-
FieldInfo[] fields = typeof(NHibernateUtil).GetFields();
29-
foreach (FieldInfo info in fields)
30-
{
31-
if (typeof(IType).IsAssignableFrom(info.FieldType) == false)
32-
continue;
33-
IType type = (IType)info.GetValue(null);
34-
clrTypeToNHibernateType[type.ReturnedClass] = type;
35-
}
36-
37-
// There are multiple possibilites for boolean, strings and datetime.
38-
// Override so that we use the most natural mapping.
39-
clrTypeToNHibernateType[Boolean.ReturnedClass] = Boolean;
40-
clrTypeToNHibernateType[String.ReturnedClass] = String;
41-
clrTypeToNHibernateType[DateTime.ReturnedClass] = DateTime;
42-
}
43-
4421
/// <summary>
4522
/// Guesses the IType of this object
4623
/// </summary>
@@ -61,8 +38,8 @@ public static IType GuessType(System.Type type)
6138
{
6239
type = type.UnwrapIfNullable();
6340

64-
IType value;
65-
if (clrTypeToNHibernateType.TryGetValue(type, out value))
41+
var value = TypeFactory.GetDefaultTypeFor(type);
42+
if (value != null)
6643
return value;
6744

6845
if (type.IsEnum)
@@ -73,7 +50,7 @@ public static IType GuessType(System.Type type)
7350
{
7451
return Custom(type);
7552
}
76-
53+
7754
return Entity(type);
7855
}
7956

@@ -401,13 +378,13 @@ public static void Initialize(object proxy)
401378
{
402379
return;
403380
}
404-
else if (proxy.IsProxy())
381+
if (proxy.IsProxy())
405382
{
406383
((INHibernateProxy)proxy).HibernateLazyInitializer.Initialize();
407384
}
408-
else if (proxy is IPersistentCollection)
385+
else if (proxy is IPersistentCollection coll)
409386
{
410-
((IPersistentCollection)proxy).ForceInitialization();
387+
coll.ForceInitialization();
411388
}
412389
}
413390

@@ -533,7 +510,7 @@ public static void Close(IEnumerator enumerator)
533510
EnumerableImpl hibernateEnumerator = enumerator as EnumerableImpl;
534511
if (hibernateEnumerator == null)
535512
{
536-
throw new ArgumentException("Not a NHibernate enumerator", "enumerator");
513+
throw new ArgumentException("Not a NHibernate enumerator", nameof(enumerator));
537514
}
538515
hibernateEnumerator.Dispose();
539516
}
@@ -547,7 +524,7 @@ public static void Close(IEnumerable enumerable)
547524
EnumerableImpl hibernateEnumerable = enumerable as EnumerableImpl;
548525
if (hibernateEnumerable == null)
549526
{
550-
throw new ArgumentException("Not a NHibernate enumerable", "enumerable");
527+
throw new ArgumentException("Not a NHibernate enumerable", nameof(enumerable));
551528
}
552529
hibernateEnumerable.Dispose();
553530
}

src/NHibernate/Type/TypeFactory.cs

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

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

617626
[MethodImpl(MethodImplOptions.Synchronized)]
618627
public static NullableType GetAnsiStringType(int length)

0 commit comments

Comments
 (0)