Skip to content

Commit d3f47d6

Browse files
hazzikoskarb
authored andcommitted
Correct call base methods on types with lazy properties (NH-3058)
1 parent 99096f8 commit d3f47d6

File tree

12 files changed

+213
-92
lines changed

12 files changed

+213
-92
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3058
2+
{
3+
public class DomainClass
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
public virtual string ALotOfText { get; set; }
8+
9+
public virtual string LoadLazyProperty()
10+
{
11+
return ALotOfText;
12+
}
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.NH3058">
4+
<class name="DomainClass">
5+
<id name="Id">
6+
<generator class="assigned" />
7+
</id>
8+
<property name="Name" />
9+
<property name="ALotOfText" lazy="true" />
10+
</class>
11+
</hibernate-mapping>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using NHibernate.Context;
2+
using NHibernate.Engine;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3058
6+
{
7+
[TestFixture]
8+
public class SampleTest : BugTestCase
9+
{
10+
public static ISessionFactoryImplementor AmbientSfi { get; private set; }
11+
12+
protected override void BuildSessionFactory()
13+
{
14+
base.BuildSessionFactory();
15+
16+
AmbientSfi = Sfi;
17+
}
18+
19+
protected override void Configure(Cfg.Configuration configuration)
20+
{
21+
base.Configure(configuration);
22+
23+
configuration.Properties.Add("current_session_context_class", "thread_static");
24+
}
25+
26+
protected override ISession OpenSession()
27+
{
28+
var session = base.OpenSession();
29+
30+
CurrentSessionContext.Bind(session);
31+
32+
return session;
33+
}
34+
35+
protected override void OnSetUp()
36+
{
37+
base.OnSetUp();
38+
using (var s = OpenSession())
39+
using (var tx = s.BeginTransaction())
40+
{
41+
var book = new DomainClass
42+
{
43+
Name = "Some name",
44+
ALotOfText = "Some text",
45+
Id = 1
46+
};
47+
48+
s.Persist(book);
49+
tx.Commit();
50+
}
51+
}
52+
53+
protected override void OnTearDown()
54+
{
55+
base.OnTearDown();
56+
using (var s = OpenSession())
57+
using (var tx = s.BeginTransaction())
58+
{
59+
Assert.That(s.CreateSQLQuery("delete from DomainClass").ExecuteUpdate(), Is.EqualTo(1));
60+
tx.Commit();
61+
}
62+
}
63+
64+
[Test]
65+
public void MethodShouldLoadLazyProperty()
66+
{
67+
using (var s = OpenSession())
68+
using (var tx = s.BeginTransaction())
69+
{
70+
var book = s.Load<DomainClass>(1);
71+
72+
Assert.False(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"));
73+
74+
string value = book.LoadLazyProperty();
75+
76+
Assert.That(value, Is.EqualTo("Some text"));
77+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
78+
79+
tx.Commit();
80+
}
81+
}
82+
}
83+
}

src/NHibernate.Test/NHSpecificTest/NH3132/Fixture.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,8 @@
55
namespace NHibernate.Test.NHSpecificTest.NH3132
66
{
77
[TestFixture]
8-
public class Fixture : TestCase
8+
public class Fixture : BugTestCase
99
{
10-
protected override string MappingsAssembly
11-
{
12-
get { return "NHibernate.Test"; }
13-
}
14-
15-
protected override IList Mappings
16-
{
17-
get
18-
{
19-
return new string[]
20-
{
21-
"NHSpecificTest.NH3132.Mappings.hbm.xml"
22-
};
23-
}
24-
}
25-
2610
/// <summary>
2711
/// push some data into the database
2812
/// Really functions as a save test also
@@ -51,12 +35,10 @@ protected override void OnTearDown()
5135
base.OnTearDown();
5236

5337
using (var session = OpenSession())
38+
using (var tran = session.BeginTransaction())
5439
{
55-
using (var tran = session.BeginTransaction())
56-
{
57-
session.Delete("from Product");
58-
tran.Commit();
59-
}
40+
session.Delete("from Product");
41+
tran.Commit();
6042
}
6143
}
6244

@@ -79,23 +61,50 @@ public void Correct_value_gets_saved()
7961
{
8062
using (var session = OpenSession())
8163
{
82-
Product product = session.CreateCriteria(typeof(Product))
64+
var product = session.CreateCriteria(typeof(Product))
8365
.Add(Restrictions.Eq("Name", "First"))
8466
.UniqueResult<Product>();
8567

86-
Assert.IsNotNull(product);
68+
Assert.That(product, Is.Not.Null);
8769
product.Name = "Changed";
8870

8971
session.Flush();
9072

9173
session.Clear();
9274

93-
Product product1 = session.CreateCriteria(typeof(Product))
75+
var product1 = session.CreateCriteria(typeof(Product))
9476
.Add(Restrictions.Eq("Name", "Changed"))
9577
.UniqueResult<Product>();
78+
79+
Assert.That(product1, Is.Not.Null);
80+
Assert.That(product1.Name, Is.EqualTo("Changed"));
81+
}
82+
}
83+
84+
[Test]
85+
public void Correct_value_gets_saved_with_lazy()
86+
{
87+
using (var session = OpenSession())
88+
{
89+
var product = session.CreateCriteria(typeof(Product))
90+
.Add(Restrictions.Eq("Name", "First"))
91+
.UniqueResult<Product>();
92+
93+
Assert.That(product, Is.Not.Null);
94+
product.Name = "Changed";
95+
product.Lazy = "LazyChanged";
96+
97+
session.Flush();
9698

97-
Assert.IsNotNull(product1);
98-
Assert.AreEqual("Changed", product1.Name);
99+
session.Clear();
100+
101+
var product1 = session.CreateCriteria(typeof(Product))
102+
.Add(Restrictions.Eq("Name", "Changed"))
103+
.UniqueResult<Product>();
104+
105+
Assert.That(product1, Is.Not.Null);
106+
Assert.That(product1.Name, Is.EqualTo("Changed"));
107+
Assert.That(product1.Lazy, Is.EqualTo("LazyChanged"));
99108
}
100109
}
101110
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@
714714
<Compile Include="NHSpecificTest\NH3374\FixtureByCode.cs" />
715715
<Compile Include="NHSpecificTest\NH2042\Model.cs" />
716716
<Compile Include="NHSpecificTest\NH2042\Fixture.cs" />
717+
<Compile Include="NHSpecificTest\NH3058\DomainClass.cs" />
718+
<Compile Include="NHSpecificTest\NH3058\SampleTest.cs" />
717719
<Compile Include="NHSpecificTest\Dates\DateTimeOffsetQueryFixture.cs" />
718720
<Compile Include="NHSpecificTest\NH3050\Fixture.cs" />
719721
<Compile Include="NHSpecificTest\NH3050\Person.cs" />
@@ -2937,6 +2939,7 @@
29372939
<EmbeddedResource Include="NHSpecificTest\NH2297\MappingsTypes.hbm.xml" />
29382940
<EmbeddedResource Include="NHSpecificTest\NH2042\Mappings.hbm.xml" />
29392941
<EmbeddedResource Include="NHSpecificTest\NH2860\Mappings.hbm.xml" />
2942+
<EmbeddedResource Include="NHSpecificTest\NH3058\Mappings.hbm.xml" />
29402943
<EmbeddedResource Include="NHSpecificTest\NH3332\Mappings.hbm.xml" />
29412944
<EmbeddedResource Include="NHSpecificTest\NH3050\Mappings.hbm.xml" />
29422945
<EmbeddedResource Include="NHSpecificTest\NH3234\Mappings.hbm.xml" />

src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,7 @@ namespace NHibernate.Intercept
77
[Serializable]
88
public class DefaultDynamicLazyFieldInterceptor : IFieldInterceptorAccessor, Proxy.DynamicProxy.IInterceptor
99
{
10-
public DefaultDynamicLazyFieldInterceptor(object targetInstance)
11-
{
12-
if (targetInstance == null)
13-
{
14-
throw new ArgumentNullException("targetInstance");
15-
}
16-
TargetInstance = targetInstance;
17-
}
18-
1910
public IFieldInterceptor FieldInterceptor { get; set; }
20-
public object TargetInstance { get; private set; }
2111

2212
public object Intercept(InvocationInfo info)
2313
{
@@ -30,7 +20,8 @@ public object Intercept(InvocationInfo info)
3020
{
3121
return FieldInterceptor;
3222
}
33-
object propValue = info.TargetMethod.Invoke(TargetInstance, info.Arguments);
23+
24+
object propValue = info.InvokeMethodOnTarget();
3425

3526
var result = FieldInterceptor.Intercept(info.Target, ReflectHelper.GetPropertyName(info.TargetMethod), propValue);
3627

@@ -59,7 +50,7 @@ public object Intercept(InvocationInfo info)
5950
}
6051
}
6152

62-
return info.TargetMethod.Invoke(TargetInstance, info.Arguments);
53+
return info.InvokeMethodOnTarget();
6354
}
6455
}
6556
}

src/NHibernate/Properties/FieldAccessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public object Get(object target)
190190
{
191191
try
192192
{
193-
return field.GetValue(target);
193+
return field.GetValue(target);
194194
}
195195
catch (Exception e)
196196
{
@@ -275,7 +275,7 @@ public void Set(object target, object value)
275275
{
276276
try
277277
{
278-
field.SetValue(target, value);
278+
field.SetValue(target, value);
279279
}
280280
catch (ArgumentException ae)
281281
{

src/NHibernate/Proxy/DefaultProxyFactory.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session
1414
{
1515
try
1616
{
17-
var initializer = new DefaultLazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod, SetIdentifierMethod,
18-
ComponentIdType, session);
17+
var initializer = new DefaultLazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType, session);
1918

2019
object proxyInstance = IsClassProxy
21-
? factory.CreateProxy(PersistentClass, initializer, Interfaces)
22-
: factory.CreateProxy(Interfaces[0], initializer, Interfaces);
20+
? factory.CreateProxy(PersistentClass, initializer, Interfaces)
21+
: factory.CreateProxy(Interfaces[0], initializer, Interfaces);
2322

2423
return (INHibernateProxy) proxyInstance;
2524
}
@@ -32,7 +31,7 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session
3231

3332
public override object GetFieldInterceptionProxy(object instanceToWrap)
3433
{
35-
var interceptor = new DefaultDynamicLazyFieldInterceptor(instanceToWrap);
34+
var interceptor = new DefaultDynamicLazyFieldInterceptor();
3635
return factory.CreateProxy(PersistentClass, interceptor, new[] { typeof(IFieldInterceptorAccessor) });
3736
}
3837
}

0 commit comments

Comments
 (0)