Skip to content

Commit 622336b

Browse files
NH-3884 - Alternate implementation without a new method.
1 parent 11a864c commit 622336b

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-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: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private enum TypeClassification
3535
}
3636

3737
private static readonly IInternalLogger _log = LoggerProvider.LoggerFor(typeof(TypeFactory));
38-
private static readonly string[] EmptyAliases= new string[0];
38+
public static readonly string[] EmptyAliases = new string[0];
3939
private static readonly char[] PrecisionScaleSplit = { '(', ')', ',' };
4040
private static readonly char[] LengthSplit = { '(', ')' };
4141

@@ -96,7 +96,15 @@ private enum TypeClassification
9696

9797
private delegate NullableType NullableTypeCreatorDelegate(SqlType sqlType);
9898

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

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-
336325
private static ICollectionTypeFactory CollectionTypeFactory =>
337326
Environment.BytecodeProvider.CollectionTypeFactory;
338327

0 commit comments

Comments
 (0)