Skip to content

Commit 3f74b75

Browse files
Clean-up IObjectsFactory usages
IObjectsFactory is meant for instantiating NHibernate dependencies, but it was used in some cases for instantiating value types. Some of its methods are also hard or impossible to implement with many dependency injection frameworks. Follow up to #1758
1 parent ba4147c commit 3f74b75

20 files changed

+143
-54
lines changed

src/NHibernate.Test/Bytecode/ActivatorObjectFactoryFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void CreateInstanceDefCtor()
4545

4646

4747

48-
[Test]
48+
[Test, Obsolete]
4949
public void CreateInstanceWithNoPublicCtor()
5050
{
5151
IObjectsFactory of = GetObjectsFactory();
@@ -55,7 +55,7 @@ public void CreateInstanceWithNoPublicCtor()
5555
Assert.That(instance, Is.InstanceOf<WithOutPublicParameterLessCtor>());
5656
}
5757

58-
[Test]
58+
[Test, Obsolete]
5959
public void CreateInstanceOfValueType()
6060
{
6161
IObjectsFactory of = GetObjectsFactory();
@@ -64,7 +64,7 @@ public void CreateInstanceOfValueType()
6464
Assert.That(instance, Is.InstanceOf<ValueType>());
6565
}
6666

67-
[Test]
67+
[Test, Obsolete]
6868
public void CreateInstanceWithArguments()
6969
{
7070
IObjectsFactory of = GetObjectsFactory();
@@ -76,4 +76,4 @@ public void CreateInstanceWithArguments()
7676
Assert.That(((WithOutPublicParameterLessCtor)instance).Something, Is.EqualTo(value));
7777
}
7878
}
79-
}
79+
}

src/NHibernate.Test/ConnectionTest/MapBasedSessionContextFixture.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Threading;
33
using NHibernate.Cfg;
44
using NHibernate.Context;
5-
using NHibernate.Engine;
65
using NUnit.Framework;
76

87
namespace NHibernate.Test.ConnectionTest
@@ -59,8 +58,6 @@ public void MapContextThreadSafety()
5958

6059
public class TestableMapBasedSessionContext : MapBasedSessionContext
6160
{
62-
public TestableMapBasedSessionContext(ISessionFactoryImplementor factory) : base(factory) { }
63-
6461
// Context is the app with such implementation. Just for the test case.
6562
internal static IDictionary _map;
6663

@@ -76,4 +73,4 @@ protected override void SetMap(IDictionary value)
7673
_map = value;
7774
}
7875
}
79-
}
76+
}

src/NHibernate.Test/ConnectionTest/ThreadLocalCurrentSessionTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using NHibernate.Cfg;
22
using NHibernate.Context;
3-
using NHibernate.Engine;
43
using NUnit.Framework;
54

65
namespace NHibernate.Test.ConnectionTest
@@ -71,8 +70,7 @@ public class TestableThreadLocalContext : ThreadLocalSessionContext
7170
{
7271
private static TestableThreadLocalContext me;
7372

74-
public TestableThreadLocalContext(ISessionFactoryImplementor factory)
75-
: base(factory)
73+
public TestableThreadLocalContext()
7674
{
7775
me = this;
7876
}
@@ -88,4 +86,4 @@ public static bool HasBind()
8886
return context != null && context.ContainsKey(me.factory);
8987
}
9088
}
91-
}
89+
}

src/NHibernate/Async/Context/ThreadLocalSessionContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
using System;
1212
using System.Collections.Generic;
1313

14-
using NHibernate;
1514
using NHibernate.Engine;
1615

1716
namespace NHibernate.Context
1817
{
1918
using System.Threading.Tasks;
2019
using System.Threading;
21-
public partial class ThreadLocalSessionContext : ICurrentSessionContext
20+
public partial class ThreadLocalSessionContext : ICurrentSessionContextWithFactory
2221
{
2322

2423
private static async Task CleanupAnyOrphanedSessionAsync(ISessionFactory factory, CancellationToken cancellationToken)

src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ private async Task ExecuteInitializedAsync(Action<string> scriptAction, bool exe
187187
cancellationToken.ThrowIfCancellationRequested();
188188
if (dialect.SupportsSqlBatches)
189189
{
190-
var objFactory = Environment.ObjectsFactory;
191-
ScriptSplitter splitter = (ScriptSplitter)objFactory.CreateInstance(typeof(ScriptSplitter), sql);
192-
193-
foreach (string stmt in splitter)
190+
foreach (var stmt in new ScriptSplitter(sql))
194191
{
195192
log.Debug("SQL Batch: {0}", stmt);
196193
cmd.CommandText = stmt;

src/NHibernate/Bytecode/ActivatorObjectsFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ public object CreateInstance(System.Type type)
99
return Activator.CreateInstance(type);
1010
}
1111

12+
// Since v5.2
13+
[Obsolete("This method has no more usages and will be removed in a future version")]
1214
public object CreateInstance(System.Type type, bool nonPublic)
1315
{
1416
return Activator.CreateInstance(type, nonPublic);
1517
}
1618

19+
// Since v5.2
20+
[Obsolete("This method has no more usages and will be removed in a future version")]
1721
public object CreateInstance(System.Type type, params object[] ctorArgs)
1822
{
1923
return Activator.CreateInstance(type, ctorArgs);
2024
}
2125
}
22-
}
26+
}

src/NHibernate/Bytecode/IObjectsFactory.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System;
2+
13
namespace NHibernate.Bytecode
24
{
35
/// <summary>
4-
/// Interface for instantiate all NHibernate objects.
6+
/// Interface for instantiating NHibernate dependencies.
57
/// </summary>
68
public interface IObjectsFactory
79
{
@@ -18,6 +20,8 @@ public interface IObjectsFactory
1820
/// <param name="type">The type of object to create.</param>
1921
/// <param name="nonPublic">true if a public or nonpublic default constructor can match; false if only a public default constructor can match.</param>
2022
/// <returns>A reference to the created object.</returns>
23+
// Since v5.2
24+
[Obsolete("This method has no more usages and will be removed in a future version")]
2125
object CreateInstance(System.Type type, bool nonPublic);
2226

2327
/// <summary>
@@ -27,6 +31,8 @@ public interface IObjectsFactory
2731
/// <param name="type">The type of object to create.</param>
2832
/// <param name="ctorArgs">An array of constructor arguments.</param>
2933
/// <returns>A reference to the created object.</returns>
34+
// Since v5.2
35+
[Obsolete("This method has no more usages and will be removed in a future version")]
3036
object CreateInstance(System.Type type, params object[] ctorArgs);
3137
}
32-
}
38+
}

src/NHibernate/Context/AsyncLocalSessionContext.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ public class AsyncLocalSessionContext : CurrentSessionContext
2121
{
2222
private readonly AsyncLocal<ISession> _session = new AsyncLocal<ISession>();
2323

24-
// Constructor signature required for dynamic invocation code.
24+
// Since v5.2
25+
[Obsolete("This constructor has no more usages and will be removed in a future version")]
2526
public AsyncLocalSessionContext(ISessionFactoryImplementor factory) { }
2627

28+
public AsyncLocalSessionContext() { }
29+
2730
protected override ISession Session
2831
{
2932
get => _session.Value;
3033
set => _session.Value = value;
3134
}
3235
}
33-
}
36+
}

src/NHibernate/Context/CallSessionContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ public class CallSessionContext : MapBasedSessionContext
2727
private static readonly AsyncLocal<IDictionary> SessionFactoryMap = new AsyncLocal<IDictionary>();
2828
#endif
2929

30+
// Since v5.2
31+
[Obsolete("This constructor has no more usages and will be removed in a future version")]
3032
public CallSessionContext(ISessionFactoryImplementor factory) : base(factory)
3133
{
3234
}
3335

36+
public CallSessionContext() { }
37+
3438
/// <summary>
3539
/// The key is the session factory and the value is the bound session.
3640
/// </summary>

src/NHibernate/Context/CurrentSessionContext.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace NHibernate.Context
1616
/// through <see cref="ISessionFactory.GetCurrentSession()"/> calls.
1717
/// </remarks>
1818
[Serializable]
19-
public abstract class CurrentSessionContext : ICurrentSessionContext
19+
public abstract class CurrentSessionContext : ICurrentSessionContextWithFactory
2020
{
2121
/// <summary> Gets or sets the currently bound session. </summary>
2222
protected abstract ISession Session { get; set; }
@@ -37,6 +37,12 @@ public virtual ISession CurrentSession()
3737
return Session;
3838
}
3939

40+
/// <inheritdoc />
41+
public virtual void SetFactory(ISessionFactoryImplementor factory)
42+
{
43+
// No-op by default.
44+
}
45+
4046
/// <summary>
4147
/// Binds the specified session to the current context.
4248
/// </summary>
@@ -86,4 +92,4 @@ private static CurrentSessionContext GetCurrentSessionContext(ISessionFactory fa
8692
return currentSessionContext;
8793
}
8894
}
89-
}
95+
}

0 commit comments

Comments
 (0)