Skip to content

Commit 91fb217

Browse files
committed
Port Hibernate's lazy attribute fetch groups
1 parent 04761c6 commit 91fb217

File tree

65 files changed

+1331
-165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1331
-165
lines changed

src/NHibernate.DomainModel/Async/CustomPersister.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ public async Task<object> LoadAsync(object id, object optionalObject, LockMode l
8585
if (obj != null)
8686
{
8787
clone = (Custom)obj.Clone();
88-
TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, false,
89-
session);
90-
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, false, session);
88+
TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, session);
89+
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, session);
9190
await (TwoPhaseLoad.InitializeEntityAsync(clone, false, session, new PreLoadEvent((IEventSource) session),
9291
new PostLoadEvent((IEventSource) session), cancellationToken));
9392
}

src/NHibernate.DomainModel/CustomPersister.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,8 @@ public object Load(object id, object optionalObject, LockMode lockMode, ISession
310310
if (obj != null)
311311
{
312312
clone = (Custom)obj.Clone();
313-
TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, false,
314-
session);
315-
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, false, session);
313+
TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, session);
314+
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, session);
316315
TwoPhaseLoad.InitializeEntity(clone, false, session, new PreLoadEvent((IEventSource) session),
317316
new PostLoadEvent((IEventSource) session));
318317
}

src/NHibernate.Test/Async/CacheTest/SerializationFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ private CacheEntry CreateCacheEntry()
142142
{
143143
DisassembledState = GetAllKnownTypeValues(),
144144
Version = 55,
145-
Subclass = "Test",
146-
AreLazyPropertiesUnfetched = true
145+
Subclass = "Test"
147146
};
148147
}
149148

@@ -240,7 +239,6 @@ private void CheckCacheEntry(CacheEntry original, CacheEntry copy)
240239
Assert.That(copy.Version, Is.EqualTo(original.Version));
241240
Assert.That(copy.Version, Is.TypeOf(original.Version.GetType()));
242241
Assert.That(copy.Subclass, Is.EqualTo(original.Subclass));
243-
Assert.That(copy.AreLazyPropertiesUnfetched, Is.EqualTo(original.AreLazyPropertiesUnfetched));
244242
for (var i = 0; i < copy.DisassembledState.Length; i++)
245243
{
246244
Assert.That(copy.DisassembledState[i], Is.TypeOf(original.DisassembledState[i].GetType()));
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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 System.Linq;
13+
using System.Text;
14+
using System.Threading.Tasks;
15+
using NHibernate.Cache;
16+
using NHibernate.Cfg;
17+
using NUnit.Framework;
18+
19+
namespace NHibernate.Test.LazyGroup
20+
{
21+
using System.Threading;
22+
[TestFixture]
23+
public class LazyGroupFixtureAsync : TestCase
24+
{
25+
protected override string MappingsAssembly => "NHibernate.Test";
26+
27+
protected override string[] Mappings => new[] { "LazyGroup.Mappings.hbm.xml" };
28+
29+
protected override void Configure(Configuration configuration)
30+
{
31+
base.Configure(configuration);
32+
configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName;
33+
configuration.Properties[Environment.UseSecondLevelCache] = "true";
34+
configuration.Properties[Environment.GenerateStatistics] = "true";
35+
}
36+
37+
protected override void OnSetUp()
38+
{
39+
using (var s = OpenSession())
40+
using (var tx = s.BeginTransaction())
41+
{
42+
for (var i = 1; i <= 5; i++)
43+
{
44+
var person = GeneratePerson(i);
45+
s.Save(person);
46+
}
47+
48+
tx.Commit();
49+
}
50+
}
51+
52+
protected override void OnTearDown()
53+
{
54+
using (var s = OpenSession())
55+
using (var tx = s.BeginTransaction())
56+
{
57+
s.CreateQuery("delete from Person").ExecuteUpdate();
58+
tx.Commit();
59+
}
60+
}
61+
62+
[Test]
63+
public async Task TestGroupsAsync()
64+
{
65+
using (var s = OpenSession())
66+
{
67+
var person = await (s.GetAsync<Person>(1));
68+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Name"), Is.True);
69+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.False);
70+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
71+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
72+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
73+
74+
var nickName = person.NickName;
75+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True);
76+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
77+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
78+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
79+
Assert.That(nickName, Is.EqualTo("NickName1"));
80+
81+
var address = person.Address;
82+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.True);
83+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
84+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
85+
Assert.That(address.City, Is.EqualTo("City1"));
86+
Assert.That(address.Street, Is.EqualTo("Street1"));
87+
Assert.That(address.PostCode, Is.EqualTo(1001));
88+
89+
var image = person.Image;
90+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.True);
91+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
92+
Assert.That(person.Image, Has.Length.EqualTo(1));
93+
94+
var age = person.Age;
95+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.True);
96+
Assert.That(person.Age, Is.EqualTo(1));
97+
}
98+
}
99+
100+
[TestCase(true)]
101+
[TestCase(false)]
102+
public async Task TestUpdateAsync(bool fetchBeforeUpdate, CancellationToken cancellationToken = default(CancellationToken))
103+
{
104+
using (var s = OpenSession())
105+
using (var tx = s.BeginTransaction())
106+
{
107+
var person = await (s.GetAsync<Person>(1, cancellationToken));
108+
if (fetchBeforeUpdate)
109+
{
110+
var nickName = person.NickName;
111+
}
112+
113+
person.NickName = "test";
114+
115+
await (tx.CommitAsync(cancellationToken));
116+
}
117+
118+
using (var s = OpenSession())
119+
using (var tx = s.BeginTransaction())
120+
{
121+
var person = await (s.GetAsync<Person>(1, cancellationToken));
122+
Assert.That(person.NickName, Is.EqualTo("test"));
123+
124+
person.NickName = "NickName1"; // reset name
125+
126+
await (tx.CommitAsync(cancellationToken));
127+
}
128+
}
129+
130+
[Test]
131+
public async Task TestCacheAsync()
132+
{
133+
var persister = Sfi.GetEntityPersister(typeof(Person).FullName);
134+
var cache = (HashtableCache) persister.Cache.Cache;
135+
await (cache.ClearAsync(CancellationToken.None));
136+
137+
using (var s = OpenSession())
138+
using (var tx = s.BeginTransaction())
139+
{
140+
var person = await (s.GetAsync<Person>(1));
141+
142+
var nickName = person.NickName;
143+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True);
144+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
145+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
146+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
147+
Assert.That(nickName, Is.EqualTo("NickName1"));
148+
149+
await (tx.CommitAsync());
150+
}
151+
152+
using (var s = OpenSession())
153+
using (var tx = s.BeginTransaction())
154+
{
155+
var person = await (s.GetAsync<Person>(1));
156+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True);
157+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
158+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
159+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
160+
Assert.That(person.NickName, Is.EqualTo("NickName1"));
161+
162+
await (tx.CommitAsync());
163+
}
164+
}
165+
166+
[Test]
167+
public async Task TestInitializeFromCacheAsync()
168+
{
169+
var persister = Sfi.GetEntityPersister(typeof(Person).FullName);
170+
var cache = (HashtableCache) persister.Cache.Cache;
171+
await (cache.ClearAsync(CancellationToken.None));
172+
Sfi.Statistics.Clear();
173+
174+
using (var s = OpenSession())
175+
using (var tx = s.BeginTransaction())
176+
{
177+
var person = await (s.GetAsync<Person>(1));
178+
179+
await (InitializeImageAsync());
180+
181+
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(2));
182+
183+
var image = person.Image; // Should be initialized from cache
184+
185+
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(2));
186+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.False);
187+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
188+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.True);
189+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
190+
Assert.That(image, Has.Length.EqualTo(1));
191+
192+
await (tx.CommitAsync());
193+
}
194+
}
195+
196+
private async Task InitializeImageAsync(CancellationToken cancellationToken = default(CancellationToken))
197+
{
198+
using (var s = OpenSession())
199+
using (var tx = s.BeginTransaction())
200+
{
201+
var person = await (s.GetAsync<Person>(1, cancellationToken));
202+
var image = person.Image;
203+
204+
await (tx.CommitAsync(cancellationToken));
205+
}
206+
}
207+
208+
private static Person GeneratePerson(int i)
209+
{
210+
return new Person
211+
{
212+
Id = i,
213+
Name = $"Person{i}",
214+
Address = new Address
215+
{
216+
City = $"City{i}",
217+
PostCode = 1000+i,
218+
Street = $"Street{i}"
219+
},
220+
Image = new byte[i],
221+
NickName = $"NickName{i}"
222+
};
223+
224+
}
225+
}
226+
}

src/NHibernate.Test/CacheTest/SerializationFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ private CacheEntry CreateCacheEntry()
131131
{
132132
DisassembledState = GetAllKnownTypeValues(),
133133
Version = 55,
134-
Subclass = "Test",
135-
AreLazyPropertiesUnfetched = true
134+
Subclass = "Test"
136135
};
137136
}
138137

@@ -229,7 +228,6 @@ private void CheckCacheEntry(CacheEntry original, CacheEntry copy)
229228
Assert.That(copy.Version, Is.EqualTo(original.Version));
230229
Assert.That(copy.Version, Is.TypeOf(original.Version.GetType()));
231230
Assert.That(copy.Subclass, Is.EqualTo(original.Subclass));
232-
Assert.That(copy.AreLazyPropertiesUnfetched, Is.EqualTo(original.AreLazyPropertiesUnfetched));
233231
for (var i = 0; i < copy.DisassembledState.Length; i++)
234232
{
235233
Assert.That(copy.DisassembledState[i], Is.TypeOf(original.DisassembledState[i].GetType()));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace NHibernate.Test.LazyGroup
8+
{
9+
public class Address
10+
{
11+
public string City { get; set; }
12+
13+
public string Street { get; set; }
14+
15+
public int PostCode { get; set; }
16+
}
17+
}

0 commit comments

Comments
 (0)