Skip to content

Clean-up IObjectsFactory usages #1781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/NHibernate.Test/Bytecode/ActivatorObjectFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void CreateInstanceDefCtor()



[Test]
[Test, Obsolete]
public void CreateInstanceWithNoPublicCtor()
{
IObjectsFactory of = GetObjectsFactory();
Expand All @@ -55,7 +55,7 @@ public void CreateInstanceWithNoPublicCtor()
Assert.That(instance, Is.InstanceOf<WithOutPublicParameterLessCtor>());
}

[Test]
[Test, Obsolete]
public void CreateInstanceOfValueType()
{
IObjectsFactory of = GetObjectsFactory();
Expand All @@ -64,7 +64,7 @@ public void CreateInstanceOfValueType()
Assert.That(instance, Is.InstanceOf<ValueType>());
}

[Test]
[Test, Obsolete]
public void CreateInstanceWithArguments()
{
IObjectsFactory of = GetObjectsFactory();
Expand All @@ -76,4 +76,4 @@ public void CreateInstanceWithArguments()
Assert.That(((WithOutPublicParameterLessCtor)instance).Something, Is.EqualTo(value));
}
}
}
}
55 changes: 55 additions & 0 deletions src/NHibernate.Test/ConnectionTest/CustomCurrentSessionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using NHibernate.Cfg;
using NHibernate.Context;
using NHibernate.Engine;
using NUnit.Framework;

namespace NHibernate.Test.ConnectionTest
{
[TestFixture]
public class CustomCurrentSessionTest : ConnectionManagementTestCase
{
protected override ISession GetSessionUnderTest()
{
var session = OpenSession();
CustomContext.Session = session;
return session;
}

protected override void Configure(Configuration configuration)
{
base.Configure(cfg);
cfg.SetProperty(Environment.CurrentSessionContextClass, typeof(CustomContext).AssemblyQualifiedName);
}

protected override void Release(ISession session)
{
CustomContext.Session = null;
base.Release(session);
}

[Test]
public void ContextIsSetup()
{
Assert.That(Sfi.CurrentSessionContext, Is.InstanceOf<CustomContext>());
Assert.That(
((CustomContext) Sfi.CurrentSessionContext).Factory,
Is.SameAs(((DebugSessionFactory) Sfi).ActualFactory));
}
}

public class CustomContext : ISessionFactoryAwareCurrentSessionContext
{
internal ISessionFactoryImplementor Factory;
internal static ISession Session;

public ISession CurrentSession()
{
return Session;
}

public void SetFactory(ISessionFactoryImplementor factory)
{
Factory = factory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ protected override void SetMap(IDictionary value)
_map = value;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ public static bool HasBind()
return context != null && context.ContainsKey(me.factory);
}
}
}
}
5 changes: 1 addition & 4 deletions src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ private async Task ExecuteInitializedAsync(Action<string> scriptAction, bool exe
cancellationToken.ThrowIfCancellationRequested();
if (dialect.SupportsSqlBatches)
{
var objFactory = Environment.ObjectsFactory;
ScriptSplitter splitter = (ScriptSplitter)objFactory.CreateInstance(typeof(ScriptSplitter), sql);

foreach (string stmt in splitter)
foreach (var stmt in new ScriptSplitter(sql))
{
log.Debug("SQL Batch: {0}", stmt);
cmd.CommandText = stmt;
Expand Down
6 changes: 5 additions & 1 deletion src/NHibernate/Bytecode/ActivatorObjectsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ public object CreateInstance(System.Type type)
return Activator.CreateInstance(type);
}

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

// Since v5.2
[Obsolete("This method has no more usages and will be removed in a future version")]
public object CreateInstance(System.Type type, params object[] ctorArgs)
{
return Activator.CreateInstance(type, ctorArgs);
}
}
}
}
10 changes: 8 additions & 2 deletions src/NHibernate/Bytecode/IObjectsFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;

namespace NHibernate.Bytecode
{
/// <summary>
/// Interface for instantiate all NHibernate objects.
/// Interface for instantiating NHibernate dependencies.
/// </summary>
public interface IObjectsFactory
{
Expand All @@ -18,6 +20,8 @@ public interface IObjectsFactory
/// <param name="type">The type of object to create.</param>
/// <param name="nonPublic">true if a public or nonpublic default constructor can match; false if only a public default constructor can match.</param>
/// <returns>A reference to the created object.</returns>
// Since v5.2
[Obsolete("This method has no more usages and will be removed in a future version")]
object CreateInstance(System.Type type, bool nonPublic);

/// <summary>
Expand All @@ -27,6 +31,8 @@ public interface IObjectsFactory
/// <param name="type">The type of object to create.</param>
/// <param name="ctorArgs">An array of constructor arguments.</param>
/// <returns>A reference to the created object.</returns>
// Since v5.2
[Obsolete("This method has no more usages and will be removed in a future version")]
object CreateInstance(System.Type type, params object[] ctorArgs);
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Context/AsyncLocalSessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ protected override ISession Session
set => _session.Value = value;
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Context/CurrentSessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ private static CurrentSessionContext GetCurrentSessionContext(ISessionFactory fa
return currentSessionContext;
}
}
}
}
19 changes: 17 additions & 2 deletions src/NHibernate/Context/ICurrentSessionContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using NHibernate.Bytecode;
using NHibernate.Engine;

namespace NHibernate.Context
Expand All @@ -12,7 +12,8 @@ namespace NHibernate.Context
/// Implementations should adhere to the following:
/// <list type="bullet">
/// <item><description>contain a constructor accepting a single argument of type
/// <see cref="ISessionFactoryImplementor" /></description></item>
/// <see cref="ISessionFactoryImplementor" />, or implement
/// <see cref="ISessionFactoryAwareCurrentSessionContext"/></description></item>
/// <item><description>should be thread safe</description></item>
/// <item><description>should be fully serializable</description></item>
/// </list>
Expand Down Expand Up @@ -42,4 +43,18 @@ public interface ICurrentSessionContext
/// locating or creating the current session.</exception>
ISession CurrentSession();
}

/// <summary>
/// An <see cref="ICurrentSessionContext"/> allowing to set its session factory. Implementing
/// this interface allows the <see cref="IObjectsFactory"/> to be used for instantiating the
/// session context.
/// </summary>
public interface ISessionFactoryAwareCurrentSessionContext : ICurrentSessionContext
{
/// <summary>
/// Sets the factory. This method should be called once after creating the context.
/// </summary>
/// <param name="factory">The factory.</param>
void SetFactory(ISessionFactoryImplementor factory);
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Context/MapBasedSessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ private ConcurrentDictionary<ISessionFactoryImplementor, ISession> GetConcreteMa
/// </summary>
protected abstract void SetMap(IDictionary value);
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Context/WcfOperationSessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ public class WcfStateExtension : IExtension<OperationContext>
public void Attach(OperationContext owner) { }
public void Detach(OperationContext owner) { }
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Context/WebSessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ protected override void SetMap(IDictionary value)
ReflectiveHttpContext.HttpContextCurrentItems[SessionFactoryMapKey] = value;
}
}
}
}
19 changes: 16 additions & 3 deletions src/NHibernate/Impl/SessionFactoryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,9 +1281,22 @@ private ICurrentSessionContext BuildCurrentSessionContext()

try
{
System.Type implClass = ReflectHelper.ClassForName(impl);
return
(ICurrentSessionContext)Environment.ObjectsFactory.CreateInstance(implClass, new object[] { this });
var implClass = ReflectHelper.ClassForName(impl);
var constructor = implClass.GetConstructor(new [] { typeof(ISessionFactoryImplementor) });
ICurrentSessionContext context;
if (constructor != null)
{
context = (ICurrentSessionContext) constructor.Invoke(new object[] { this });
}
else
{
context = (ICurrentSessionContext) Environment.ObjectsFactory.CreateInstance(implClass);
}
if (context is ISessionFactoryAwareCurrentSessionContext sessionFactoryAwareContext)
{
sessionFactoryAwareContext.SetFactory(this);
}
return context;
}
catch (Exception e)
{
Expand Down
5 changes: 1 addition & 4 deletions src/NHibernate/Tool/hbm2ddl/SchemaExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,7 @@ private void ExecuteSql(DbCommand cmd, string sql)
{
if (dialect.SupportsSqlBatches)
{
var objFactory = Environment.ObjectsFactory;
ScriptSplitter splitter = (ScriptSplitter)objFactory.CreateInstance(typeof(ScriptSplitter), sql);

foreach (string stmt in splitter)
foreach (var stmt in new ScriptSplitter(sql))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on #1758, the ScriptSplitter does not expose anything overridable, and so it cannot benefit from being instantiated by the object factory: it cannot supply a custom implementation.

{
log.Debug("SQL Batch: {0}", stmt);
cmd.CommandText = stmt;
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Transform/AliasToBeanResultTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public override object TransformTuple(object[] tuple, String[] aliases)
{
result = _resultClass.IsClass
? _beanConstructor.Invoke(null)
: Cfg.Environment.ObjectsFactory.CreateInstance(_resultClass, true);
: Activator.CreateInstance(_resultClass, true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in #1758, this usage is not a dependency but a DTO instantiation. If the user wants some custom behavior for instantiating value types, he now would have to provide its own transformer.


for (int i = 0; i < aliases.Length; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Tuple/PocoInstantiator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private object GetInstance()
}
if (mappedClass.IsValueType)
{
return Cfg.Environment.ObjectsFactory.CreateInstance(mappedClass, true);
return Activator.CreateInstance(mappedClass, true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in #1758, this usage is not a dependency but an entity instantiation. If the user wants some custom behavior for instantiating value types, he now would have to provide its own tuplizer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that the true here is required.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably in some partial trust or similar environments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just preferred to be quite conservative, inlining the default previous implementation "as is".

}
if (constructor == null)
{
Expand Down