Skip to content

Commit 6d02db3

Browse files
NH-3884 - Alternate implementation without a new method.
1 parent 6dfa0b7 commit 6d02db3

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

src/NHibernate.Test/TypesTest/ChangeDefaultTypeFixture.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ protected override void Configure(Configuration configuration)
2525
_originalDefaultDateTimeType = TypeFactory.GetDefaultTypeFor(typeof(DateTime));
2626
Assert.That(_originalDefaultDateTimeType, Is.Not.Null);
2727
_testDefaultDateTimeType = NHibernateUtil.DateTime.Equals(_originalDefaultDateTimeType)
28-
? (IType) NHibernateUtil.Timestamp
28+
? (IType) NHibernateUtil.DateTimeNoMs
2929
: NHibernateUtil.DateTime;
30-
TypeFactory.SetDefaultType<DateTime>(_testDefaultDateTimeType);
30+
TypeFactory.RegisterType(typeof(DateTime), _testDefaultDateTimeType, TypeFactory.EmptyAliases);
3131
base.Configure(configuration);
3232
}
3333

3434
protected override void DropSchema()
3535
{
3636
base.DropSchema();
3737
if (_originalDefaultDateTimeType != null)
38-
TypeFactory.SetDefaultType<DateTime>(_originalDefaultDateTimeType);
38+
TypeFactory.RegisterType(typeof(DateTime), _originalDefaultDateTimeType, TypeFactory.EmptyAliases);
3939
}
4040

4141
[Test]
@@ -72,13 +72,13 @@ public void ParameterType()
7272
var q = s.CreateQuery($"from {nameof(ChangeDefaultTypeClass)} where :date1 = :date2 or :date1 = :date3")
7373
.SetParameter("date1", DateTime.Now)
7474
.SetDateTime("date2", DateTime.Now)
75-
.SetTimestamp("date3", DateTime.Now);
75+
.SetDateTimeNoMs("date3", DateTime.Now);
7676

7777
var namedParameters = namedParametersField.GetValue(q) as Dictionary<string, TypedValue>;
7878
Assert.That(namedParameters, Is.Not.Null, "Unable to retrieve parameters internal field");
7979
Assert.That(namedParameters["date1"].Type, Is.EqualTo(_testDefaultDateTimeType));
8080
Assert.That(namedParameters["date2"].Type, Is.EqualTo(NHibernateUtil.DateTime));
81-
Assert.That(namedParameters["date3"].Type, Is.EqualTo(NHibernateUtil.Timestamp));
81+
Assert.That(namedParameters["date3"].Type, Is.EqualTo(NHibernateUtil.DateTimeNoMs));
8282
}
8383
}
8484
}

src/NHibernate/Type/TypeFactory.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ private enum TypeClassification
3434
PrecisionScale
3535
}
3636

37+
public static readonly string[] EmptyAliases = System.Array.Empty<string>();
38+
3739
private static readonly INHibernateLogger _log = NHibernateLogger.For(typeof(TypeFactory));
38-
private static readonly string[] EmptyAliases= System.Array.Empty<string>();
3940
private static readonly char[] PrecisionScaleSplit = { '(', ')', ',' };
4041
private static readonly char[] LengthSplit = { '(', ')' };
4142

@@ -96,7 +97,15 @@ private enum TypeClassification
9697

9798
private delegate NullableType NullableTypeCreatorDelegate(SqlType sqlType);
9899

99-
private static void RegisterType(System.Type systemType, IType nhibernateType, IEnumerable<string> aliases)
100+
/// <summary>
101+
/// <para>Defines which NHibernate type should be chosen by default for handling a given .Net type.</para>
102+
/// <para>This must be done before any operation on NHibernate, including building its
103+
/// <see cref="Configuration" /> and building session factory. Otherwise the behavior will be undefined.</para>
104+
/// </summary>
105+
/// <param name="systemType">The .Net type.</param>
106+
/// <param name="nhibernateType">The NHibernate type.</param>
107+
/// <param name="aliases">The additional aliases to map to the type. Use <see cref="EmptyAliases"/> if none.</param>
108+
public static void RegisterType(System.Type systemType, IType nhibernateType, IEnumerable<string> aliases)
100109
{
101110
var typeAliases = new List<string>(aliases);
102111
typeAliases.AddRange(GetClrTypeAliases(systemType));
@@ -314,25 +323,6 @@ private static void RegisterBuiltInTypes()
314323
len => new SerializableType(typeof (object), SqlTypeFactory.GetBinary(len))));
315324
}
316325

317-
/// <summary>
318-
/// <para>Defines which NHibernate type should be chosen by default for handling a given .Net type.</para>
319-
/// <para>This must be done before any operation on NHibernate, including building its
320-
/// <see cref="Configuration" /> and building session factory. Otherwise the behavior will be undefined.</para>
321-
/// </summary>
322-
/// <param name="targetType">The NHibernate type.</param>
323-
/// <typeparam name="T">The .Net type.</typeparam>
324-
public static void SetDefaultType<T>(IType targetType)
325-
{
326-
if (targetType == null)
327-
throw new ArgumentNullException(nameof(targetType));
328-
329-
var type = typeof(T);
330-
foreach (var alias in GetClrTypeAliases(type))
331-
{
332-
typeByTypeOfName[alias] = targetType;
333-
}
334-
}
335-
336326
private static ICollectionTypeFactory CollectionTypeFactory =>
337327
Environment.BytecodeProvider.CollectionTypeFactory;
338328

0 commit comments

Comments
 (0)