Skip to content

Commit 70c948e

Browse files
maca88fredericDelaporte
authored andcommitted
Partially port Hibernate's current field interceptor mechanism (#1947)
1 parent 5ba22d8 commit 70c948e

17 files changed

+403
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 NHibernate.Bytecode;
13+
using NUnit.Framework;
14+
15+
namespace NHibernate.Test.GhostProperty
16+
{
17+
using System.Threading.Tasks;
18+
// Since 5.3
19+
[Obsolete]
20+
[TestFixture]
21+
public class GhostPropertyDynamicProxyFixtureAsync : GhostPropertyFixtureAsync
22+
{
23+
private string _originalProxyFactoryFactory;
24+
25+
protected override void Configure(Cfg.Configuration configuration)
26+
{
27+
base.Configure(configuration);
28+
_originalProxyFactoryFactory = Cfg.Environment.BytecodeProvider.ProxyFactoryFactory.GetType().FullName;
29+
configuration.SetProperty(Cfg.Environment.ProxyFactoryFactoryClass, typeof(DefaultProxyFactoryFactory).FullName);
30+
}
31+
32+
protected override void DropSchema()
33+
{
34+
base.DropSchema();
35+
// Reset IProxyFactoryFactory back to default
36+
var injectableProxyFactory = (IInjectableProxyFactoryFactory) Cfg.Environment.BytecodeProvider;
37+
injectableProxyFactory.SetProxyFactoryFactory(_originalProxyFactoryFactory);
38+
}
39+
}
40+
}

src/NHibernate.Test/Async/GhostProperty/GhostPropertyFixture.cs

+83
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ protected override void OnSetUp()
4747
Id = 1
4848
};
4949
s.Persist(wireTransfer);
50+
var creditCard = new CreditCard
51+
{
52+
Id = 2
53+
};
54+
s.Persist(creditCard);
5055
s.Persist(new Order
5156
{
5257
Id = 1,
@@ -89,6 +94,71 @@ public async Task CanGetActualValueFromLazyManyToOneAsync()
8994
}
9095
}
9196

97+
[Test]
98+
public async Task CanGetInitializedLazyManyToOneAfterClosedSessionAsync()
99+
{
100+
Order order;
101+
Payment payment;
102+
103+
using (var s = OpenSession())
104+
{
105+
order = await (s.GetAsync<Order>(1));
106+
payment = order.Payment; // Initialize Payment
107+
}
108+
109+
Assert.That(order.Payment, Is.EqualTo(payment));
110+
Assert.That(order.Payment is WireTransfer, Is.True);
111+
}
112+
113+
[Test]
114+
public async Task InitializedLazyManyToOneBeforeParentShouldNotBeAProxyAsync()
115+
{
116+
Order order;
117+
Payment payment;
118+
119+
using (var s = OpenSession())
120+
{
121+
payment = await (s.LoadAsync<Payment>(1));
122+
await (NHibernateUtil.InitializeAsync(payment));
123+
order = await (s.GetAsync<Order>(1));
124+
// Here the Payment property should be unwrapped
125+
payment = order.Payment;
126+
}
127+
128+
Assert.That(order.Payment, Is.EqualTo(payment));
129+
Assert.That(order.Payment is WireTransfer, Is.True);
130+
}
131+
132+
[Test]
133+
public async Task SetUninitializedProxyShouldNotTriggerPropertyInitializationAsync()
134+
{
135+
using (var s = OpenSession())
136+
{
137+
var order = await (s.GetAsync<Order>(1));
138+
Assert.That(order.Payment is WireTransfer, Is.True); // Load property
139+
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
140+
order.Payment = await (s.LoadAsync<Payment>(2));
141+
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
142+
Assert.That(NHibernateUtil.IsInitialized(order.Payment), Is.False);
143+
Assert.That(order.Payment is WireTransfer, Is.False);
144+
}
145+
}
146+
147+
[Test]
148+
public async Task SetInitializedProxyShouldNotResetPropertyInitializationAsync()
149+
{
150+
using (var s = OpenSession())
151+
{
152+
var order = await (s.GetAsync<Order>(1));
153+
var payment = await (s.LoadAsync<Payment>(2));
154+
Assert.That(order.Payment is WireTransfer, Is.True); // Load property
155+
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
156+
await (NHibernateUtil.InitializeAsync(payment));
157+
order.Payment = payment;
158+
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
159+
}
160+
}
161+
92162
[Test]
93163
public async Task WillNotLoadGhostPropertyByDefaultAsync()
94164
{
@@ -183,5 +253,18 @@ public async Task AcceptPropertySetWithTransientObjectAsync()
183253

184254
Assert.That(order.Payment, Is.EqualTo(newPayment));
185255
}
256+
257+
[Test]
258+
public async Task WillFetchJoinInSingleHqlQueryAsync()
259+
{
260+
Order order = null;
261+
262+
using (ISession s = OpenSession())
263+
{
264+
order = (await (s.CreateQuery("from Order o left join fetch o.Payment where o.Id = 1").ListAsync<Order>()))[0];
265+
}
266+
267+
Assert.DoesNotThrow(() => { var x = order.Payment; });
268+
}
186269
}
187270
}

src/NHibernate.Test/Async/LazyOneToOne/LazyOneToOneTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public async Task LazyAsync()
7878

7979
s = OpenSession();
8080
t = s.BeginTransaction();
81-
p = await (s.GetAsync<Person>("Gavin"));
82-
Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Employee"));
81+
p = await (s.GetAsync<Person>("Gavin")); // The default loader will fetch the employee
82+
Assert.That(NHibernateUtil.IsPropertyInitialized(p, "Employee"));
8383

8484
Assert.That(p.Employee.Person, Is.SameAs(p));
8585
Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
@@ -95,4 +95,4 @@ public async Task LazyAsync()
9595
s.Close();
9696
}
9797
}
98-
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using NHibernate.Bytecode;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.GhostProperty
6+
{
7+
// Since 5.3
8+
[Obsolete]
9+
[TestFixture]
10+
public class GhostPropertyDynamicProxyFixture : GhostPropertyFixture
11+
{
12+
private string _originalProxyFactoryFactory;
13+
14+
protected override void Configure(Cfg.Configuration configuration)
15+
{
16+
base.Configure(configuration);
17+
_originalProxyFactoryFactory = Cfg.Environment.BytecodeProvider.ProxyFactoryFactory.GetType().FullName;
18+
configuration.SetProperty(Cfg.Environment.ProxyFactoryFactoryClass, typeof(DefaultProxyFactoryFactory).FullName);
19+
}
20+
21+
protected override void DropSchema()
22+
{
23+
base.DropSchema();
24+
// Reset IProxyFactoryFactory back to default
25+
var injectableProxyFactory = (IInjectableProxyFactoryFactory) Cfg.Environment.BytecodeProvider;
26+
injectableProxyFactory.SetProxyFactoryFactory(_originalProxyFactoryFactory);
27+
}
28+
}
29+
}

src/NHibernate.Test/GhostProperty/GhostPropertyFixture.cs

+83
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ protected override void OnSetUp()
3636
Id = 1
3737
};
3838
s.Persist(wireTransfer);
39+
var creditCard = new CreditCard
40+
{
41+
Id = 2
42+
};
43+
s.Persist(creditCard);
3944
s.Persist(new Order
4045
{
4146
Id = 1,
@@ -84,6 +89,71 @@ public void CanGetActualValueFromLazyManyToOne()
8489
}
8590
}
8691

92+
[Test]
93+
public void CanGetInitializedLazyManyToOneAfterClosedSession()
94+
{
95+
Order order;
96+
Payment payment;
97+
98+
using (var s = OpenSession())
99+
{
100+
order = s.Get<Order>(1);
101+
payment = order.Payment; // Initialize Payment
102+
}
103+
104+
Assert.That(order.Payment, Is.EqualTo(payment));
105+
Assert.That(order.Payment is WireTransfer, Is.True);
106+
}
107+
108+
[Test]
109+
public void InitializedLazyManyToOneBeforeParentShouldNotBeAProxy()
110+
{
111+
Order order;
112+
Payment payment;
113+
114+
using (var s = OpenSession())
115+
{
116+
payment = s.Load<Payment>(1);
117+
NHibernateUtil.Initialize(payment);
118+
order = s.Get<Order>(1);
119+
// Here the Payment property should be unwrapped
120+
payment = order.Payment;
121+
}
122+
123+
Assert.That(order.Payment, Is.EqualTo(payment));
124+
Assert.That(order.Payment is WireTransfer, Is.True);
125+
}
126+
127+
[Test]
128+
public void SetUninitializedProxyShouldNotTriggerPropertyInitialization()
129+
{
130+
using (var s = OpenSession())
131+
{
132+
var order = s.Get<Order>(1);
133+
Assert.That(order.Payment is WireTransfer, Is.True); // Load property
134+
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
135+
order.Payment = s.Load<Payment>(2);
136+
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
137+
Assert.That(NHibernateUtil.IsInitialized(order.Payment), Is.False);
138+
Assert.That(order.Payment is WireTransfer, Is.False);
139+
}
140+
}
141+
142+
[Test]
143+
public void SetInitializedProxyShouldNotResetPropertyInitialization()
144+
{
145+
using (var s = OpenSession())
146+
{
147+
var order = s.Get<Order>(1);
148+
var payment = s.Load<Payment>(2);
149+
Assert.That(order.Payment is WireTransfer, Is.True); // Load property
150+
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
151+
NHibernateUtil.Initialize(payment);
152+
order.Payment = payment;
153+
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
154+
}
155+
}
156+
87157
[Test]
88158
public void WillNotLoadGhostPropertyByDefault()
89159
{
@@ -178,5 +248,18 @@ public void AcceptPropertySetWithTransientObject()
178248

179249
Assert.That(order.Payment, Is.EqualTo(newPayment));
180250
}
251+
252+
[Test]
253+
public void WillFetchJoinInSingleHqlQuery()
254+
{
255+
Order order = null;
256+
257+
using (ISession s = OpenSession())
258+
{
259+
order = s.CreateQuery("from Order o left join fetch o.Payment where o.Id = 1").List<Order>()[0];
260+
}
261+
262+
Assert.DoesNotThrow(() => { var x = order.Payment; });
263+
}
181264
}
182265
}

src/NHibernate.Test/LazyOneToOne/LazyOneToOneTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void Lazy()
6767

6868
s = OpenSession();
6969
t = s.BeginTransaction();
70-
p = s.Get<Person>("Gavin");
71-
Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Employee"));
70+
p = s.Get<Person>("Gavin"); // The default loader will fetch the employee
71+
Assert.That(NHibernateUtil.IsPropertyInitialized(p, "Employee"));
7272

7373
Assert.That(p.Employee.Person, Is.SameAs(p));
7474
Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
@@ -84,4 +84,4 @@ public void Lazy()
8484
s.Close();
8585
}
8686
}
87-
}
87+
}

src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public void CanSerializeFieldInterceptorProxy()
423423
{
424424
var factory = new StaticProxyFactory();
425425
factory.PostInstantiate(typeof(PublicInterfaceTestClass).FullName, typeof(PublicInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
426-
var proxy = (PublicInterfaceTestClass) factory.GetFieldInterceptionProxy(new PublicInterfaceTestClass());
426+
var proxy = (PublicInterfaceTestClass) factory.GetFieldInterceptionProxy();
427427
proxy.Id = 1;
428428

429429
var serializer = GetFormatter();
@@ -441,7 +441,7 @@ public void CanSerializeFieldInterceptorProxyWithISerializableEntity()
441441
{
442442
var factory = new StaticProxyFactory();
443443
factory.PostInstantiate(typeof(CustomSerializationClass).FullName, typeof(CustomSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
444-
var proxy = (CustomSerializationClass) factory.GetFieldInterceptionProxy(new CustomSerializationClass());
444+
var proxy = (CustomSerializationClass) factory.GetFieldInterceptionProxy();
445445
proxy.Id = 2;
446446

447447
var serializer = GetFormatter();
@@ -459,7 +459,7 @@ public void CanSerializeFieldInterceptorProxyWithExplicitISerializableEntity()
459459
{
460460
var factory = new StaticProxyFactory();
461461
factory.PostInstantiate(typeof(CustomExplicitSerializationClass).FullName, typeof(CustomExplicitSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
462-
var proxy = (CustomExplicitSerializationClass) factory.GetFieldInterceptionProxy(new CustomExplicitSerializationClass());
462+
var proxy = (CustomExplicitSerializationClass) factory.GetFieldInterceptionProxy();
463463
proxy.Id = 2;
464464

465465
var serializer = GetFormatter();
@@ -482,7 +482,7 @@ public void VerifyFieldInterceptorProxy()
482482
() =>
483483
{
484484
#endif
485-
var fieldProxy = factory.GetFieldInterceptionProxy(new InternalInterfaceTestClass());
485+
var fieldProxy = factory.GetFieldInterceptionProxy();
486486
Assert.That(fieldProxy, Is.InstanceOf<InternalInterfaceTestClass>());
487487
#if NETFX
488488
});
@@ -499,7 +499,7 @@ public void VerifyFieldInterceptorProxyWithISerializableEntity()
499499
() =>
500500
{
501501
#endif
502-
var fieldProxy = factory.GetFieldInterceptionProxy(new CustomSerializationClass());
502+
var fieldProxy = factory.GetFieldInterceptionProxy();
503503
Assert.That(fieldProxy, Is.InstanceOf<CustomSerializationClass>());
504504
#if NETFX
505505
});
@@ -527,7 +527,7 @@ public void VerifyFieldInterceptorProxyWithAdditionalInterface()
527527
() =>
528528
{
529529
#endif
530-
var fieldProxy = factory.GetFieldInterceptionProxy(new PublicInterfaceTestClass());
530+
var fieldProxy = factory.GetFieldInterceptionProxy();
531531
Assert.That(fieldProxy, Is.InstanceOf<PublicInterfaceTestClass>());
532532
#if NETFX
533533
});

0 commit comments

Comments
 (0)