Skip to content

Commit dc71c7d

Browse files
rjpereshazzik
andcommitted
NH-3670 - Dynamic component should allow generic dictionary (#305)
Fixes #755 Co-authored-by: Alexander Zaytsev <hazzik@gmail.com>
1 parent 25ff3db commit dc71c7d

File tree

30 files changed

+1403
-35
lines changed

30 files changed

+1403
-35
lines changed

doc/reference/modules/basic_mapping.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,8 @@
20812081

20822082
<para>
20832083
The <literal>&lt;dynamic-component&gt;</literal> element allows an <literal>IDictionary</literal>
2084-
to be mapped as a component, where the property names refer to keys of the dictionary.
2084+
or <literal>IDictionary<string, object></literal>to be mapped as a component, where the property
2085+
names refer to keys of the dictionary.
20852086
</para>
20862087

20872088
</sect2>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using NUnit.Framework;
14+
15+
16+
namespace NHibernate.Test.NHSpecificTest.NH1039Generic
17+
{
18+
using System.Threading.Tasks;
19+
[TestFixture]
20+
public class FixtureAsync : BugTestCase
21+
{
22+
public override string BugNumber
23+
{
24+
get { return "NH1039Generic"; }
25+
}
26+
27+
protected override void OnTearDown()
28+
{
29+
base.OnTearDown();
30+
using (ISession s = OpenSession())
31+
using (ITransaction tx = s.BeginTransaction())
32+
{
33+
s.Delete("from Person");
34+
tx.Commit();
35+
}
36+
}
37+
38+
[Test]
39+
public async Task testAsync()
40+
{
41+
using (ISession s = OpenSession())
42+
using (ITransaction tx = s.BeginTransaction())
43+
{
44+
Person person = new Person("1");
45+
person.Name = "John Doe";
46+
var set = new HashSet<object>();
47+
set.Add("555-1234");
48+
set.Add("555-4321");
49+
person.Properties.Add("Phones", set);
50+
51+
await (s.SaveAsync(person));
52+
await (tx.CommitAsync());
53+
}
54+
using (ISession s = OpenSession())
55+
using (ITransaction tx = s.BeginTransaction())
56+
{
57+
Person person = (Person)await (s.CreateCriteria(typeof(Person)).UniqueResultAsync());
58+
59+
Assert.AreEqual("1", person.ID);
60+
Assert.AreEqual("John Doe", person.Name);
61+
Assert.AreEqual(1, person.Properties.Count);
62+
Assert.That(person.Properties["Phones"], Is.InstanceOf<ISet<object>>());
63+
Assert.IsTrue(((ISet<object>) person.Properties["Phones"]).Contains("555-1234"));
64+
Assert.IsTrue(((ISet<object>) person.Properties["Phones"]).Contains("555-4321"));
65+
}
66+
}
67+
}
68+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections.Generic;
12+
using NUnit.Framework;
13+
14+
namespace NHibernate.Test.NHSpecificTest.NH1796Generic
15+
{
16+
using System.Threading.Tasks;
17+
[TestFixture]
18+
public class FixtureAsync: BugTestCase
19+
{
20+
[Test]
21+
public async Task MergeAsync()
22+
{
23+
var entity = new Entity { Name = "Vinnie Luther" };
24+
using (ISession s = OpenSession())
25+
using (ITransaction t = s.BeginTransaction())
26+
{
27+
await (s.SaveAsync(entity));
28+
await (t.CommitAsync());
29+
}
30+
31+
entity.DynProps = new Dictionary<string, object>();
32+
entity.DynProps["StrProp"] = "Modified";
33+
using (ISession s = OpenSession())
34+
using (ITransaction t = s.BeginTransaction())
35+
{
36+
await (s.MergeAsync(entity));
37+
await (t.CommitAsync());
38+
}
39+
40+
using (ISession s = OpenSession())
41+
using (ITransaction t = s.BeginTransaction())
42+
{
43+
await (s.CreateQuery("delete from Entity").ExecuteUpdateAsync());
44+
await (t.CommitAsync());
45+
}
46+
}
47+
48+
[Test]
49+
public async Task SaveOrUpdateAsync()
50+
{
51+
var entity = new Entity { Name = "Vinnie Luther" };
52+
using (ISession s = OpenSession())
53+
using (ITransaction t = s.BeginTransaction())
54+
{
55+
await (s.SaveOrUpdateAsync(entity));
56+
await (t.CommitAsync());
57+
}
58+
59+
entity.DynProps = new Dictionary<string, object>();
60+
entity.DynProps["StrProp"] = "Modified";
61+
using (ISession s = OpenSession())
62+
using (ITransaction t = s.BeginTransaction())
63+
{
64+
await (s.SaveOrUpdateAsync(entity));
65+
await (t.CommitAsync());
66+
}
67+
68+
using (ISession s = OpenSession())
69+
using (ITransaction t = s.BeginTransaction())
70+
{
71+
await (s.CreateQuery("delete from Entity").ExecuteUpdateAsync());
72+
await (t.CommitAsync());
73+
}
74+
}
75+
}
76+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections;
12+
using System.Linq;
13+
using NHibernate.Linq;
14+
using NUnit.Framework;
15+
using System.Linq.Expressions;
16+
using System;
17+
18+
namespace NHibernate.Test.NHSpecificTest.NH2664Generic
19+
{
20+
using System.Threading.Tasks;
21+
[TestFixture]
22+
public class FixtureAsync : TestCase
23+
{
24+
protected override string MappingsAssembly => "NHibernate.Test";
25+
26+
protected override IList Mappings => new[] {"NHSpecificTest.NH2664Generic.Mappings.hbm.xml"};
27+
28+
/// <summary>
29+
/// push some data into the database
30+
/// Really functions as a save test also
31+
/// </summary>
32+
protected override void OnSetUp()
33+
{
34+
using (var session = OpenSession())
35+
using (var tran = session.BeginTransaction())
36+
{
37+
session.Save(
38+
new Product
39+
{
40+
ProductId = "1",
41+
Properties =
42+
{
43+
["Name"] = "First Product",
44+
["Description"] = "First Description"
45+
}
46+
});
47+
48+
session.Save(
49+
new Product
50+
{
51+
ProductId = "2",
52+
Properties =
53+
{
54+
["Name"] = "Second Product",
55+
["Description"] = "Second Description"
56+
}
57+
});
58+
59+
session.Save(
60+
new Product
61+
{
62+
ProductId = "3",
63+
Properties =
64+
{
65+
["Name"] = "val",
66+
["Description"] = "val"
67+
}
68+
});
69+
70+
tran.Commit();
71+
}
72+
}
73+
74+
protected override void OnTearDown()
75+
{
76+
using (var session = OpenSession())
77+
using (var tran = session.BeginTransaction())
78+
{
79+
session.CreateQuery("delete from Product").ExecuteUpdate();
80+
tran.Commit();
81+
}
82+
}
83+
84+
[Test]
85+
public async Task Query_DynamicComponentAsync()
86+
{
87+
using (var session = OpenSession())
88+
{
89+
var product = await ((
90+
from p in session.Query<Product>()
91+
where (string) p.Properties["Name"] == "First Product"
92+
select p
93+
).SingleAsync());
94+
95+
Assert.That(product, Is.Not.Null);
96+
Assert.That(product.Properties["Name"], Is.EqualTo("First Product"));
97+
}
98+
}
99+
100+
[Test]
101+
public async Task Multiple_Query_Does_Not_CacheAsync()
102+
{
103+
using (var session = OpenSession())
104+
{
105+
// Query by name
106+
var product1 = await ((
107+
from p in session.Query<Product>()
108+
where (string) p.Properties["Name"] == "First Product"
109+
select p
110+
).SingleAsync());
111+
Assert.That(product1.ProductId, Is.EqualTo("1"));
112+
113+
// Query by description (this test is to verify that the dictionary
114+
// index isn't cached from the query above.
115+
var product2 = await ((
116+
from p in session.Query<Product>()
117+
where (string) p.Properties["Description"] == "Second Description"
118+
select p
119+
).SingleAsync());
120+
Assert.That(product2.ProductId, Is.EqualTo("2"));
121+
}
122+
}
123+
}
124+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections;
12+
using System.Linq;
13+
using NHibernate.Linq;
14+
using NUnit.Framework;
15+
using System.Linq.Expressions;
16+
using System;
17+
18+
namespace NHibernate.Test.NHSpecificTest.NH3571Generic
19+
{
20+
using System.Threading.Tasks;
21+
[TestFixture]
22+
public class FixtureAsync : TestCase
23+
{
24+
protected override string MappingsAssembly => "NHibernate.Test";
25+
26+
protected override IList Mappings => new[] {"NHSpecificTest.NH3571Generic.Mappings.hbm.xml"};
27+
28+
/// <summary>
29+
/// push some data into the database
30+
/// Really functions as a save test also
31+
/// </summary>
32+
protected override void OnSetUp()
33+
{
34+
using (var session = OpenSession())
35+
using (var tran = session.BeginTransaction())
36+
{
37+
var product = new Product {ProductId = "1"};
38+
product.Details.Properties["Name"] = "First Product";
39+
product.Details.Properties["Description"] = "First Description";
40+
41+
session.Save(product);
42+
43+
product = new Product {ProductId = "2"};
44+
product.Details.Properties["Name"] = "Second Product";
45+
product.Details.Properties["Description"] = "Second Description";
46+
47+
session.Save(product);
48+
49+
product = new Product {ProductId = "3"};
50+
product.Details.Properties["Name"] = "val";
51+
product.Details.Properties["Description"] = "val";
52+
53+
session.Save(product);
54+
55+
tran.Commit();
56+
}
57+
}
58+
59+
protected override void OnTearDown()
60+
{
61+
using (var session = OpenSession())
62+
using (var tran = session.BeginTransaction())
63+
{
64+
session.CreateQuery("delete from Product").ExecuteUpdate();
65+
tran.Commit();
66+
}
67+
}
68+
69+
[Test]
70+
public async Task CanQueryDynamicComponentInComponentAsync()
71+
{
72+
using (var session = OpenSession())
73+
{
74+
var product = await ((
75+
from p in session.Query<Product>()
76+
where (string) p.Details.Properties["Name"] == "First Product"
77+
select p
78+
).SingleAsync());
79+
80+
Assert.That(product, Is.Not.Null);
81+
Assert.That(product.Details.Properties["Name"], Is.EqualTo("First Product"));
82+
}
83+
}
84+
85+
[Test]
86+
public async Task MultipleQueriesShouldNotCacheAsync()
87+
{
88+
using (var session = OpenSession())
89+
{
90+
// Query by name
91+
var product1 = await ((
92+
from p in session.Query<Product>()
93+
where (string) p.Details.Properties["Name"] == "First Product"
94+
select p
95+
).SingleAsync());
96+
Assert.That(product1.ProductId, Is.EqualTo("1"));
97+
98+
// Query by description (this test is to verify that the dictionary
99+
// index isn't cached from the query above.
100+
var product2 = await ((
101+
from p in session.Query<Product>()
102+
where (string) p.Details.Properties["Description"] == "Second Description"
103+
select p
104+
).SingleAsync());
105+
Assert.That(product2.ProductId, Is.EqualTo("2"));
106+
}
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)