Skip to content

Commit ed9102c

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 nhibernate#1758
1 parent 97dc207 commit ed9102c

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+
}

src/NHibernate/Context/ICurrentSessionContext.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using NHibernate.Engine;
32

43
namespace NHibernate.Context
@@ -42,4 +41,17 @@ public interface ICurrentSessionContext
4241
/// locating or creating the current session.</exception>
4342
ISession CurrentSession();
4443
}
44+
45+
// 6.0 TODO: merge into ICurrentSessionContext
46+
/// <summary>
47+
/// Transitional interface for <see cref="ICurrentSessionContext"/>.
48+
/// </summary>
49+
public interface ICurrentSessionContextWithFactory : ICurrentSessionContext
50+
{
51+
/// <summary>
52+
/// Sets the factory. This method should be called once after creating the context.
53+
/// </summary>
54+
/// <param name="factory">The factory.</param>
55+
void SetFactory(ISessionFactoryImplementor factory);
56+
}
4557
}

src/NHibernate/Context/MapBasedSessionContext.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Concurrent;
34
using NHibernate.Engine;
@@ -6,16 +7,28 @@ namespace NHibernate.Context
67
{
78
public abstract class MapBasedSessionContext : CurrentSessionContext
89
{
9-
private readonly ISessionFactoryImplementor _factory;
10+
private ISessionFactoryImplementor _factory;
1011

1112
// Must be static, different instances of MapBasedSessionContext may have to yield the same map.
1213
private static readonly object _locker = new object();
1314

15+
// Since v5.2
16+
[Obsolete("This constructor has no more usages and will be removed in a future version")]
1417
protected MapBasedSessionContext(ISessionFactoryImplementor factory)
1518
{
1619
_factory = factory;
1720
}
1821

22+
protected MapBasedSessionContext() { }
23+
24+
/// <inheritdoc />
25+
public override void SetFactory(ISessionFactoryImplementor factory)
26+
{
27+
if (_factory != null)
28+
throw new InvalidOperationException("The factory has already been set");
29+
_factory = factory;
30+
}
31+
1932
/// <summary>
2033
/// Gets or sets the currently bound session.
2134
/// </summary>
@@ -64,4 +77,4 @@ private ConcurrentDictionary<ISessionFactoryImplementor, ISession> GetConcreteMa
6477
/// </summary>
6578
protected abstract void SetMap(IDictionary value);
6679
}
67-
}
80+
}

src/NHibernate/Context/ThreadLocalSessionContext.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33

4-
using NHibernate;
54
using NHibernate.Engine;
65

76
namespace NHibernate.Context
@@ -31,21 +30,25 @@ namespace NHibernate.Context
3130
/// <para>The cleanup on transaction end is indeed not implemented.</para>
3231
/// </summary>
3332
[Serializable]
34-
public partial class ThreadLocalSessionContext : ICurrentSessionContext
33+
public partial class ThreadLocalSessionContext : ICurrentSessionContextWithFactory
3534
{
3635
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(ThreadLocalSessionContext));
3736

3837
[ThreadStatic]
3938
protected static IDictionary<ISessionFactory, ISession> context;
4039

41-
protected readonly ISessionFactoryImplementor factory;
40+
protected ISessionFactoryImplementor factory;
4241

4342

43+
// Since v5.2
44+
[Obsolete("This constructor has no more usages and will be removed in a future version")]
4445
public ThreadLocalSessionContext(ISessionFactoryImplementor factory)
4546
{
4647
this.factory = factory;
4748
}
4849

50+
public ThreadLocalSessionContext() { }
51+
4952
#region ICurrentSessionContext Members
5053

5154
public ISession CurrentSession()
@@ -66,6 +69,14 @@ public ISession CurrentSession()
6669
return current;
6770
}
6871

72+
/// <inheritdoc />
73+
public void SetFactory(ISessionFactoryImplementor factory)
74+
{
75+
if (this.factory != null)
76+
throw new InvalidOperationException("The factory has already been set");
77+
this.factory = factory;
78+
}
79+
6980
#endregion
7081

7182
private static void CleanupAnyOrphanedSession(ISessionFactory factory)

src/NHibernate/Context/ThreadStaticSessionContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ public class ThreadStaticSessionContext : MapBasedSessionContext
1414
[ThreadStatic]
1515
private static IDictionary _map;
1616

17+
// Since v5.2
18+
[Obsolete("This constructor has no more usages and will be removed in a future version")]
1719
public ThreadStaticSessionContext(ISessionFactoryImplementor factory) : base (factory) { }
1820

21+
public ThreadStaticSessionContext() { }
22+
1923
protected override IDictionary GetMap()
2024
{
2125
return _map;

src/NHibernate/Context/WcfOperationSessionContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ namespace NHibernate.Context
1212
/// </summary>
1313
public class WcfOperationSessionContext : MapBasedSessionContext
1414
{
15+
// Since v5.2
16+
[Obsolete("This constructor has no more usages and will be removed in a future version")]
1517
public WcfOperationSessionContext(ISessionFactoryImplementor factory) : base(factory) {}
1618

19+
public WcfOperationSessionContext() { }
20+
1721
private static WcfStateExtension WcfOperationState
1822
{
1923
get
@@ -49,4 +53,4 @@ public class WcfStateExtension : IExtension<OperationContext>
4953
public void Attach(OperationContext owner) { }
5054
public void Detach(OperationContext owner) { }
5155
}
52-
}
56+
}

src/NHibernate/Context/WebSessionContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ public class WebSessionContext : MapBasedSessionContext
1313
{
1414
private const string SessionFactoryMapKey = "NHibernate.Context.WebSessionContext.SessionFactoryMapKey";
1515

16+
// Since v5.2
17+
[Obsolete("This constructor has no more usages and will be removed in a future version")]
1618
public WebSessionContext(ISessionFactoryImplementor factory) : base(factory) {}
1719

20+
public WebSessionContext() { }
21+
1822
protected override IDictionary GetMap()
1923
{
2024
return ReflectiveHttpContext.HttpContextCurrentItems[SessionFactoryMapKey] as IDictionary;
@@ -25,4 +29,4 @@ protected override void SetMap(IDictionary value)
2529
ReflectiveHttpContext.HttpContextCurrentItems[SessionFactoryMapKey] = value;
2630
}
2731
}
28-
}
32+
}

0 commit comments

Comments
 (0)