Skip to content
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

Allow generic dictionaries for dynamic entities #1767

Merged

Conversation

fredericDelaporte
Copy link
Member

Follow-up to #755

Generic dictionaries could already be used for creating dynamic entities, but not for working with dynamic entities loaded from the database.

Possible breaking change: querying a dynamic entity as a Hashtable instead of an IDictionary is no more supported.

hazzik
hazzik previously approved these changes Jun 21, 2018
@@ -50,7 +50,7 @@ public object Instantiate()

protected virtual IDictionary GenerateMap()
{
return new Hashtable();
return new Dictionary<string, object>();
Copy link
Member

@hazzik hazzik Jun 21, 2018

Choose a reason for hiding this comment

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

I think it's better to create a new type DynamicEntityInstantiator in case anyone has subclassed from that. And make this class obsolete.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why not.

And obsolete the old one

To be squashed.
public object Instantiate()
{
var map = GenerateMap();
if (_entityName != null)
Copy link
Member

Choose a reason for hiding this comment

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

This is always 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.

All NHibernate paths always assign a non-null EntityName to PersistentClass, but PersistentClass does not guarantee it will never be null. Now for the purpose here, we do not need a special handling of null. If EntityName is null, the old code would have added it to _isInstanceEntityNames nonetheless.

{
if (!(obj is IDictionary<string, object> that))
return false;
if (_entityName == null)
Copy link
Member

Choose a reason for hiding this comment

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

This is always false.

if (_entityName == null)
return true;

var type = (string) that[Key];
Copy link
Member

@hazzik hazzik Jun 21, 2018

Choose a reason for hiding this comment

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

That could fail. There is a difference between IDictionary and IDictionary<,>:

var dictionary = new Dictionary<string, object>();
IDictionary a = dictionary;
var x = a["SomeThing"]; // x is null
IDictionary<string, object> b = dictionary;
var y = b["SomeThing"]; // KeyNotFoundException

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes my bad, forgot about it.

return true;

var type = (string) that[Key];
return type == null || _isInstanceEntityNames.Contains(type);
Copy link
Member

@hazzik hazzik Jun 21, 2018

Choose a reason for hiding this comment

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

This condition is likely to be incorrect. In my mind: if type == null then this dictionary is not an instance of the entity type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed.

Coded too fast...

To be squashed.
@fredericDelaporte fredericDelaporte merged commit af319b6 into nhibernate:master Jun 25, 2018
@fredericDelaporte fredericDelaporte deleted the DynamicEntities branch June 25, 2018 09:22
@hazzik
Copy link
Member

hazzik commented Jun 26, 2018

I just realised that dynamic entities are not queryable by Linq.

@fredericDelaporte
Copy link
Member Author

fredericDelaporte commented Jun 26, 2018

This change was done for criteria and HQL, as showcased by the added tests. Does the lack of Linq support have any impact on what should have been done?

@hazzik
Copy link
Member

hazzik commented Jun 26, 2018

No, it’s all fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants