-
Notifications
You must be signed in to change notification settings - Fork 934
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
Clean-up IObjectsFactory usages #1781
Clean-up IObjectsFactory usages #1781
Conversation
ScriptSplitter splitter = (ScriptSplitter)objFactory.CreateInstance(typeof(ScriptSplitter), sql); | ||
|
||
foreach (string stmt in splitter) | ||
foreach (var stmt in new ScriptSplitter(sql)) |
There was a problem hiding this comment.
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.
@@ -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); |
There was a problem hiding this comment.
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.
@@ -103,7 +103,7 @@ private object GetInstance() | |||
} | |||
if (mappedClass.IsValueType) | |||
{ | |||
return Cfg.Environment.ObjectsFactory.CreateInstance(mappedClass, true); | |||
return Activator.CreateInstance(mappedClass, true); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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".
@@ -1261,35 +1261,65 @@ private void Init() | |||
|
|||
private ICurrentSessionContext BuildCurrentSessionContext() | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not change this method and would not change the standard contexts' constructors. Also I would make the ICurrentSessionContextWithFactory
interface optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this method is not changed then ICurrentSessionContextWithFactory
would not have any usage and should not be kept.
So, do you mean you would not change just the direct instantiation cases?
About making ICurrentSessionContextWithFactory
optional, then the obsoleted line:
(ICurrentSessionContext) Environment.ObjectsFactory.CreateInstance(implClass, this);
Would have to be replaced by:
(ICurrentSessionContext) Activator.CreateInstance(implClass, this);
In order to avoid leaving a non-obsoleted feature depending on an obsoleted one.
38c4923
to
0c2f139
Compare
This comment has been minimized.
This comment has been minimized.
Shall we go further (in other PR) and replace |
Yes, we should. |
|
||
var context = (ISessionFactoryAwareCurrentSessionContext) Environment.ObjectsFactory.CreateInstance(implClass); | ||
context.SetFactory(this); | ||
return context; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking a lot about this chunk of code, but could not come up with a great solution, unfortunately.
So, I came up with 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);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, changed for this. (And rebased.)
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
Do an alternate cleanup for session context
Rename the added interface
Use session context creation logic from @hazzik Co-authored-by: Alexander Zaytsev <hazzik@gmail.com>
09d9902
to
009648f
Compare
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
Possible breaking change:
AliasToBeanResultTransformer
with value types, or their own entity tuplizer if they were using value types as entities.ICurrentSessionContextWithFactory
and add a parameterless public constructor to their custom context, and move their custom instantiation logic fromIObjectsFactory.CreateInstance(Type, object[])
toIObjectsFactory.CreateInstance(Type)
.