Skip to content

Allow to use dynamic objects as dynamic components #1778

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
merged 2 commits into from
Oct 30, 2018
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
7 changes: 4 additions & 3 deletions doc/reference/modules/basic_mapping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2680,9 +2680,10 @@
</para>

<para>
The <literal>&lt;dynamic-component&gt;</literal> element allows an <literal>IDictionary</literal>
or <literal>IDictionary&lt;string, object&gt;</literal> to be mapped as a component, where the
property names refer to keys of the dictionary. See <xref linkend="components-dynamic" />.
The <literal>&lt;dynamic-component&gt;</literal> element allows an <literal>IDictionary</literal>,
<literal>IDictionary&lt;string, object&gt;</literal>, or a C# <literal>dynamic</literal> to be mapped
as a component. When using dictionaries, the property names refer to keys of the dictionary. See
<xref linkend="components-dynamic" />.
</para>

</sect2>
Expand Down
3 changes: 2 additions & 1 deletion doc/reference/modules/component_mapping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@

<para>
You can also map a property of type <literal>IDictionary</literal> or
<literal>IDictionary&lt;string, object&gt;</literal>:
<literal>IDictionary&lt;string, object&gt;</literal>, or declared as a C#
<literal>dynamic</literal>:
</para>

<programlisting><![CDATA[<dynamic-component name="UserAttributes">
Expand Down
64 changes: 64 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1039Dynamic/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH1039Dynamic
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Delete("from Person");
tx.Commit();
}
}

[Test]
public async Task TestAsync()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var person = new Person("1")
{
Name = "John Doe",
Properties =
{
Phones = new HashSet<object> {"555-1234", "555-4321"}
}
};

await (s.SaveAsync(person));
await (tx.CommitAsync());
}

using (var s = OpenSession())
using (s.BeginTransaction())
{
var person = (Person) await (s.CreateCriteria(typeof(Person)).UniqueResultAsync());

Assert.That(person.ID, Is.EqualTo("1"));
Assert.That(person.Name, Is.EqualTo("John Doe"));
var phones = person.Properties.Phones as ISet<object>;
Assert.That(phones, Is.Not.Null);
Assert.That(phones.Contains("555-1234"), Is.True);
Assert.That(phones.Contains("555-4321"), Is.True);
}
}
}
}
118 changes: 118 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH2664Dynamic/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Collections;
using System.Linq;
using NUnit.Framework;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Dynamic.Core;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.NH2664Dynamic
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : TestCase
{
protected override string MappingsAssembly => "NHibernate.Test";

protected override string[] Mappings => new[] {"NHSpecificTest.NH2664Dynamic.Mappings.hbm.xml"};

/// <summary>
/// push some data into the database
/// Really functions as a save test also
/// </summary>
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var properties1 = new Dictionary<string, object>
{
["Name"] = "First Product",
["Description"] = "First Description"
};
session.Save(
new Product
{
ProductId = "1",
Properties = properties1
});

var properties2 = new
{
Name = "Second Product",
Description = "Second Description"
};
session.Save(
new Product
{
ProductId = "2",
Properties = properties2
});

dynamic properties3 = new ExpandoObject();
properties3.Name = "Third Product";
properties3.Description = "Third Description";
session.Save(
new Product
{
ProductId = "3",
Properties = properties3
});

tran.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.CreateQuery("delete from Product").ExecuteUpdate();
tran.Commit();
}
}

[Test]
public async Task Query_DynamicComponentAsync()
{
using (var session = OpenSession())
{
var product = await (session
.Query<Product>()
.Where("Properties.Name == @0", "First Product")
.SingleAsync());

Assert.That(product, Is.Not.Null);
Assert.That((object) product.Properties["Name"], Is.EqualTo("First Product"));
Assert.That((object) product.Properties.Name, Is.EqualTo("First Product"));
}
}

[Test]
public async Task Multiple_Query_Does_Not_CacheAsync()
{
using (var session = OpenSession())
{
// Query by name
var product1 = await (session.Query<Product>().Where("Properties.Name == @0", "First Product").SingleAsync());
Assert.That(product1.ProductId, Is.EqualTo("1"));

// Query by description (this test is to verify that the dictionary
// index isn't cached from the query above.
var product2 = await (session.Query<Product>().Where("Properties.Description == @0", "Second Description").SingleAsync());
Assert.That(product2.ProductId, Is.EqualTo("2"));
}
}
}
}
53 changes: 53 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1039Dynamic/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH1039Dynamic
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Delete("from Person");
tx.Commit();
}
}

[Test]
public void Test()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var person = new Person("1")
{
Name = "John Doe",
Properties =
{
Phones = new HashSet<object> {"555-1234", "555-4321"}
}
};

s.Save(person);
tx.Commit();
}

using (var s = OpenSession())
using (s.BeginTransaction())
{
var person = (Person) s.CreateCriteria(typeof(Person)).UniqueResult();

Assert.That(person.ID, Is.EqualTo("1"));
Assert.That(person.Name, Is.EqualTo("John Doe"));
var phones = person.Properties.Phones as ISet<object>;
Assert.That(phones, Is.Not.Null);
Assert.That(phones.Contains("555-1234"), Is.True);
Assert.That(phones.Contains("555-4321"), Is.True);
}
}
}
}
21 changes: 21 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1039Dynamic/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernate.Test.NHSpecificTest.NH1039Dynamic"
assembly="NHibernate.Test">

<class name="Person" table="NH1039_Person">
<id name="ID" type="string" length="32">
<generator class="assigned"/>
</id>

<property name="Name"/>

<dynamic-component name="Properties">
<set name="Phones" table="NH1039_Phone">
<key column="PersonId"/>
<element column="`Number`" type="string"/>
</set>
</dynamic-component>
</class>

</hibernate-mapping>
26 changes: 26 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1039Dynamic/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;

namespace NHibernate.Test.NHSpecificTest.NH1039Dynamic
{
public class Person
{
public Person()
{
}

public Person(string id)
{
ID = id;
}

public virtual string ID { get; set; }

public virtual string Name { get; set; }

public virtual dynamic Properties { get; set; } = new ExpandoObject();
}
}
Loading