Skip to content

Commit 1e00fef

Browse files
fredericDelaportehazzik
authored andcommitted
Allow naming a property FieldInterceptor when an entity has lazy properties (#1444)
Fixes #1442
1 parent a4e7da2 commit 1e00fef

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
using System.Collections;
12+
using NHibernate.Intercept;
1213
using NHibernate.Tuple.Entity;
1314
using NUnit.Framework;
1415

@@ -42,14 +43,20 @@ protected override DebugSessionFactory BuildSessionFactory()
4243

4344
protected override void OnSetUp()
4445
{
46+
Assert.That(
47+
nameof(Book.FieldInterceptor),
48+
Is.EqualTo(nameof(IFieldInterceptorAccessor.FieldInterceptor)),
49+
$"Test pre-condition not met: entity property {nameof(Book.FieldInterceptor)} should have the same " +
50+
$"name than {nameof(IFieldInterceptorAccessor)}.{nameof(IFieldInterceptorAccessor.FieldInterceptor)}");
4551
using (var s = OpenSession())
4652
using (var tx = s.BeginTransaction())
4753
{
4854
s.Persist(new Book
4955
{
5056
Name = "some name",
5157
Id = 1,
52-
ALotOfText = "a lot of text ..."
58+
ALotOfText = "a lot of text ...",
59+
FieldInterceptor = "Why not that name?"
5360
});
5461
tx.Commit();
5562
}
@@ -76,12 +83,14 @@ public async Task PropertyLoadedNotInitializedAsync()
7683
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.False);
7784
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.False);
7885
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
86+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.False);
7987

8088
await (NHibernateUtil.InitializeAsync(book));
8189

8290
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
8391
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
8492
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
93+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True);
8594
}
8695
}
8796

@@ -95,6 +104,7 @@ public async Task PropertyLoadedNotInitializedWhenUsingGetAsync()
95104
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
96105
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
97106
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
107+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True);
98108
}
99109
}
100110

@@ -118,6 +128,7 @@ public async Task CanGetValueForNonLazyPropertyAsync()
118128
var book = await (s.GetAsync<Book>(1));
119129

120130
Assert.That(book.Name, Is.EqualTo("some name"));
131+
Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?"));
121132
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
122133
}
123134
}
@@ -137,7 +148,6 @@ public async Task CanLoadAndSaveObjectInDifferentSessionsAsync()
137148
}
138149
}
139150

140-
141151
[Test]
142152
public async Task CanUpdateNonLazyWithoutLoadingLazyPropertyAsync()
143153
{
@@ -147,17 +157,19 @@ public async Task CanUpdateNonLazyWithoutLoadingLazyPropertyAsync()
147157
{
148158
book = await (s.GetAsync<Book>(1));
149159
book.Name += "updated";
160+
book.FieldInterceptor += "updated";
150161

151-
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
162+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "Before flush and commit");
152163
await (trans.CommitAsync());
164+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "After flush and commit");
153165
}
154166

155-
156167
using (ISession s = OpenSession())
157168
{
158169
book = await (s.GetAsync<Book>(1));
159170
Assert.That(book.Name, Is.EqualTo("some nameupdated"));
171+
Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?updated"));
160172
}
161173
}
162174
}
163-
}
175+
}

src/NHibernate.Test/LazyProperty/Book.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ public virtual string ALotOfText
1212
get { return _aLotOfText; }
1313
set { _aLotOfText = value; }
1414
}
15+
16+
public virtual string FieldInterceptor { get; set; }
1517
}
16-
}
18+
}

src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using NHibernate.Intercept;
23
using NHibernate.Tuple.Entity;
34
using NUnit.Framework;
45

@@ -31,14 +32,20 @@ protected override DebugSessionFactory BuildSessionFactory()
3132

3233
protected override void OnSetUp()
3334
{
35+
Assert.That(
36+
nameof(Book.FieldInterceptor),
37+
Is.EqualTo(nameof(IFieldInterceptorAccessor.FieldInterceptor)),
38+
$"Test pre-condition not met: entity property {nameof(Book.FieldInterceptor)} should have the same " +
39+
$"name than {nameof(IFieldInterceptorAccessor)}.{nameof(IFieldInterceptorAccessor.FieldInterceptor)}");
3440
using (var s = OpenSession())
3541
using (var tx = s.BeginTransaction())
3642
{
3743
s.Persist(new Book
3844
{
3945
Name = "some name",
4046
Id = 1,
41-
ALotOfText = "a lot of text ..."
47+
ALotOfText = "a lot of text ...",
48+
FieldInterceptor = "Why not that name?"
4249
});
4350
tx.Commit();
4451
}
@@ -65,12 +72,14 @@ public void PropertyLoadedNotInitialized()
6572
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.False);
6673
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.False);
6774
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
75+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.False);
6876

6977
NHibernateUtil.Initialize(book);
7078

7179
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
7280
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
7381
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
82+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True);
7483
}
7584
}
7685

@@ -90,6 +99,7 @@ public void PropertyLoadedNotInitializedWhenUsingGet()
9099
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
91100
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
92101
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
102+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True);
93103
}
94104
}
95105

@@ -113,6 +123,7 @@ public void CanGetValueForNonLazyProperty()
113123
var book = s.Get<Book>(1);
114124

115125
Assert.That(book.Name, Is.EqualTo("some name"));
126+
Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?"));
116127
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
117128
}
118129
}
@@ -132,7 +143,6 @@ public void CanLoadAndSaveObjectInDifferentSessions()
132143
}
133144
}
134145

135-
136146
[Test]
137147
public void CanUpdateNonLazyWithoutLoadingLazyProperty()
138148
{
@@ -142,17 +152,19 @@ public void CanUpdateNonLazyWithoutLoadingLazyProperty()
142152
{
143153
book = s.Get<Book>(1);
144154
book.Name += "updated";
155+
book.FieldInterceptor += "updated";
145156

146-
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
157+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "Before flush and commit");
147158
trans.Commit();
159+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "After flush and commit");
148160
}
149161

150-
151162
using (ISession s = OpenSession())
152163
{
153164
book = s.Get<Book>(1);
154165
Assert.That(book.Name, Is.EqualTo("some nameupdated"));
166+
Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?updated"));
155167
}
156168
}
157169
}
158-
}
170+
}

src/NHibernate.Test/LazyProperty/Mappings.hbm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</id>
1010
<property name="Name" />
1111
<property name="ALotOfText" lazy="true" />
12+
<property name="FieldInterceptor" />
1213
</class>
1314

1415
</hibernate-mapping>

src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public object Intercept(InvocationInfo info)
1717
{
1818
if (ReflectHelper.IsPropertyGet(info.TargetMethod))
1919
{
20-
if("get_FieldInterceptor".Equals(methodName))
20+
if (IsGetFieldInterceptorCall(methodName, info.TargetMethod))
2121
{
2222
return FieldInterceptor;
2323
}
@@ -33,7 +33,7 @@ public object Intercept(InvocationInfo info)
3333
}
3434
else if (ReflectHelper.IsPropertySet(info.TargetMethod))
3535
{
36-
if ("set_FieldInterceptor".Equals(methodName))
36+
if (IsSetFieldInterceptorCall(methodName, info.TargetMethod))
3737
{
3838
FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
3939
return null;
@@ -44,7 +44,7 @@ public object Intercept(InvocationInfo info)
4444
}
4545
else
4646
{
47-
if ("set_FieldInterceptor".Equals(methodName))
47+
if (IsSetFieldInterceptorCall(methodName, info.TargetMethod))
4848
{
4949
FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
5050
return null;
@@ -53,5 +53,15 @@ public object Intercept(InvocationInfo info)
5353

5454
return info.InvokeMethodOnTarget();
5555
}
56+
57+
private static bool IsGetFieldInterceptorCall(string methodName, MethodInfo targetMethod)
58+
{
59+
return "get_FieldInterceptor".Equals(methodName) && targetMethod.DeclaringType == typeof(IFieldInterceptorAccessor);
60+
}
61+
62+
private static bool IsSetFieldInterceptorCall(string methodName, MethodInfo targetMethod)
63+
{
64+
return "set_FieldInterceptor".Equals(methodName) && targetMethod.DeclaringType == typeof(IFieldInterceptorAccessor);
65+
}
5666
}
5767
}

0 commit comments

Comments
 (0)