Skip to content

Partially port Hibernate's current field interceptor mechanism #1947

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 8 commits into from
Jan 11, 2019
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//------------------------------------------------------------------------------
// <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 NHibernate.Bytecode;
using NUnit.Framework;

namespace NHibernate.Test.GhostProperty
{
using System.Threading.Tasks;
// Since 5.3
[Obsolete]
[TestFixture]
public class GhostPropertyDynamicProxyFixtureAsync : GhostPropertyFixtureAsync
{
private string _originalProxyFactoryFactory;

protected override void Configure(Cfg.Configuration configuration)
{
base.Configure(configuration);
_originalProxyFactoryFactory = Cfg.Environment.BytecodeProvider.ProxyFactoryFactory.GetType().FullName;
configuration.SetProperty(Cfg.Environment.ProxyFactoryFactoryClass, typeof(DefaultProxyFactoryFactory).FullName);
}

protected override void DropSchema()
{
base.DropSchema();
// Reset IProxyFactoryFactory back to default
var injectableProxyFactory = (IInjectableProxyFactoryFactory) Cfg.Environment.BytecodeProvider;
injectableProxyFactory.SetProxyFactoryFactory(_originalProxyFactoryFactory);
}
}
}
83 changes: 83 additions & 0 deletions src/NHibernate.Test/Async/GhostProperty/GhostPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ protected override void OnSetUp()
Id = 1
};
s.Persist(wireTransfer);
var creditCard = new CreditCard
{
Id = 2
};
s.Persist(creditCard);
s.Persist(new Order
{
Id = 1,
Expand Down Expand Up @@ -89,6 +94,71 @@ public async Task CanGetActualValueFromLazyManyToOneAsync()
}
}

[Test]
public async Task CanGetInitializedLazyManyToOneAfterClosedSessionAsync()
{
Order order;
Payment payment;

using (var s = OpenSession())
{
order = await (s.GetAsync<Order>(1));
payment = order.Payment; // Initialize Payment
}

Assert.That(order.Payment, Is.EqualTo(payment));
Assert.That(order.Payment is WireTransfer, Is.True);
}

[Test]
public async Task InitializedLazyManyToOneBeforeParentShouldNotBeAProxyAsync()
{
Order order;
Payment payment;

using (var s = OpenSession())
{
payment = await (s.LoadAsync<Payment>(1));
await (NHibernateUtil.InitializeAsync(payment));
order = await (s.GetAsync<Order>(1));
// Here the Payment property should be unwrapped
payment = order.Payment;
}

Assert.That(order.Payment, Is.EqualTo(payment));
Assert.That(order.Payment is WireTransfer, Is.True);
}

[Test]
public async Task SetUninitializedProxyShouldNotTriggerPropertyInitializationAsync()
{
using (var s = OpenSession())
{
var order = await (s.GetAsync<Order>(1));
Assert.That(order.Payment is WireTransfer, Is.True); // Load property
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
order.Payment = await (s.LoadAsync<Payment>(2));
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
Assert.That(NHibernateUtil.IsInitialized(order.Payment), Is.False);
Assert.That(order.Payment is WireTransfer, Is.False);
}
}

[Test]
public async Task SetInitializedProxyShouldNotResetPropertyInitializationAsync()
{
using (var s = OpenSession())
{
var order = await (s.GetAsync<Order>(1));
var payment = await (s.LoadAsync<Payment>(2));
Assert.That(order.Payment is WireTransfer, Is.True); // Load property
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
await (NHibernateUtil.InitializeAsync(payment));
order.Payment = payment;
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
}
}

[Test]
public async Task WillNotLoadGhostPropertyByDefaultAsync()
{
Expand Down Expand Up @@ -183,5 +253,18 @@ public async Task AcceptPropertySetWithTransientObjectAsync()

Assert.That(order.Payment, Is.EqualTo(newPayment));
}

[Test]
public async Task WillFetchJoinInSingleHqlQueryAsync()
{
Order order = null;

using (ISession s = OpenSession())
{
order = (await (s.CreateQuery("from Order o left join fetch o.Payment where o.Id = 1").ListAsync<Order>()))[0];
}

Assert.DoesNotThrow(() => { var x = order.Payment; });
}
}
}
6 changes: 3 additions & 3 deletions src/NHibernate.Test/Async/LazyOneToOne/LazyOneToOneTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public async Task LazyAsync()

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

Assert.That(p.Employee.Person, Is.SameAs(p));
Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
Expand All @@ -95,4 +95,4 @@ public async Task LazyAsync()
s.Close();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using NHibernate.Bytecode;
using NUnit.Framework;

namespace NHibernate.Test.GhostProperty
{
// Since 5.3
[Obsolete]
[TestFixture]
public class GhostPropertyDynamicProxyFixture : GhostPropertyFixture
{
private string _originalProxyFactoryFactory;

protected override void Configure(Cfg.Configuration configuration)
{
base.Configure(configuration);
_originalProxyFactoryFactory = Cfg.Environment.BytecodeProvider.ProxyFactoryFactory.GetType().FullName;
configuration.SetProperty(Cfg.Environment.ProxyFactoryFactoryClass, typeof(DefaultProxyFactoryFactory).FullName);
}

protected override void DropSchema()
{
base.DropSchema();
// Reset IProxyFactoryFactory back to default
var injectableProxyFactory = (IInjectableProxyFactoryFactory) Cfg.Environment.BytecodeProvider;
injectableProxyFactory.SetProxyFactoryFactory(_originalProxyFactoryFactory);
}
}
}
83 changes: 83 additions & 0 deletions src/NHibernate.Test/GhostProperty/GhostPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ protected override void OnSetUp()
Id = 1
};
s.Persist(wireTransfer);
var creditCard = new CreditCard
{
Id = 2
};
s.Persist(creditCard);
s.Persist(new Order
{
Id = 1,
Expand Down Expand Up @@ -84,6 +89,71 @@ public void CanGetActualValueFromLazyManyToOne()
}
}

[Test]
public void CanGetInitializedLazyManyToOneAfterClosedSession()
{
Order order;
Payment payment;

using (var s = OpenSession())
{
order = s.Get<Order>(1);
payment = order.Payment; // Initialize Payment
}

Assert.That(order.Payment, Is.EqualTo(payment));
Assert.That(order.Payment is WireTransfer, Is.True);
}

[Test]
public void InitializedLazyManyToOneBeforeParentShouldNotBeAProxy()
{
Order order;
Payment payment;

using (var s = OpenSession())
{
payment = s.Load<Payment>(1);
NHibernateUtil.Initialize(payment);
order = s.Get<Order>(1);
// Here the Payment property should be unwrapped
payment = order.Payment;
}

Assert.That(order.Payment, Is.EqualTo(payment));
Assert.That(order.Payment is WireTransfer, Is.True);
}

[Test]
public void SetUninitializedProxyShouldNotTriggerPropertyInitialization()
{
using (var s = OpenSession())
{
var order = s.Get<Order>(1);
Assert.That(order.Payment is WireTransfer, Is.True); // Load property
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
order.Payment = s.Load<Payment>(2);
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
Assert.That(NHibernateUtil.IsInitialized(order.Payment), Is.False);
Assert.That(order.Payment is WireTransfer, Is.False);
}
}

[Test]
public void SetInitializedProxyShouldNotResetPropertyInitialization()
{
using (var s = OpenSession())
{
var order = s.Get<Order>(1);
var payment = s.Load<Payment>(2);
Assert.That(order.Payment is WireTransfer, Is.True); // Load property
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
NHibernateUtil.Initialize(payment);
order.Payment = payment;
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "Payment"), Is.True);
}
}

[Test]
public void WillNotLoadGhostPropertyByDefault()
{
Expand Down Expand Up @@ -178,5 +248,18 @@ public void AcceptPropertySetWithTransientObject()

Assert.That(order.Payment, Is.EqualTo(newPayment));
}

[Test]
public void WillFetchJoinInSingleHqlQuery()
{
Order order = null;

using (ISession s = OpenSession())
{
order = s.CreateQuery("from Order o left join fetch o.Payment where o.Id = 1").List<Order>()[0];
}

Assert.DoesNotThrow(() => { var x = order.Payment; });
}
}
}
6 changes: 3 additions & 3 deletions src/NHibernate.Test/LazyOneToOne/LazyOneToOneTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public void Lazy()

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

Assert.That(p.Employee.Person, Is.SameAs(p));
Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
Expand All @@ -84,4 +84,4 @@ public void Lazy()
s.Close();
}
}
}
}
12 changes: 6 additions & 6 deletions src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public void CanSerializeFieldInterceptorProxy()
{
var factory = new StaticProxyFactory();
factory.PostInstantiate(typeof(PublicInterfaceTestClass).FullName, typeof(PublicInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
var proxy = (PublicInterfaceTestClass) factory.GetFieldInterceptionProxy(new PublicInterfaceTestClass());
var proxy = (PublicInterfaceTestClass) factory.GetFieldInterceptionProxy();
proxy.Id = 1;

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

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

var serializer = GetFormatter();
Expand All @@ -482,7 +482,7 @@ public void VerifyFieldInterceptorProxy()
() =>
{
#endif
var fieldProxy = factory.GetFieldInterceptionProxy(new InternalInterfaceTestClass());
var fieldProxy = factory.GetFieldInterceptionProxy();
Assert.That(fieldProxy, Is.InstanceOf<InternalInterfaceTestClass>());
#if NETFX
});
Expand All @@ -499,7 +499,7 @@ public void VerifyFieldInterceptorProxyWithISerializableEntity()
() =>
{
#endif
var fieldProxy = factory.GetFieldInterceptionProxy(new CustomSerializationClass());
var fieldProxy = factory.GetFieldInterceptionProxy();
Assert.That(fieldProxy, Is.InstanceOf<CustomSerializationClass>());
#if NETFX
});
Expand Down Expand Up @@ -527,7 +527,7 @@ public void VerifyFieldInterceptorProxyWithAdditionalInterface()
() =>
{
#endif
var fieldProxy = factory.GetFieldInterceptionProxy(new PublicInterfaceTestClass());
var fieldProxy = factory.GetFieldInterceptionProxy();
Assert.That(fieldProxy, Is.InstanceOf<PublicInterfaceTestClass>());
#if NETFX
});
Expand Down
Loading