Skip to content

Allow naming an entity property FieldInterceptor when it has lazy properties #1444

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 2 commits into from
Nov 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


using System.Collections;
using NHibernate.Intercept;
using NHibernate.Tuple.Entity;
using NUnit.Framework;

Expand Down Expand Up @@ -42,14 +43,20 @@ protected override DebugSessionFactory BuildSessionFactory()

protected override void OnSetUp()
{
Assert.That(
nameof(Book.FieldInterceptor),
Is.EqualTo(nameof(IFieldInterceptorAccessor.FieldInterceptor)),
$"Test pre-condition not met: entity property {nameof(Book.FieldInterceptor)} should have the same " +
$"name than {nameof(IFieldInterceptorAccessor)}.{nameof(IFieldInterceptorAccessor.FieldInterceptor)}");
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Persist(new Book
{
Name = "some name",
Id = 1,
ALotOfText = "a lot of text ..."
ALotOfText = "a lot of text ...",
FieldInterceptor = "Why not that name?"
});
tx.Commit();
}
Expand All @@ -76,12 +83,14 @@ public async Task PropertyLoadedNotInitializedAsync()
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.False);

await (NHibernateUtil.InitializeAsync(book));

Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True);
}
}

Expand All @@ -95,6 +104,7 @@ public async Task PropertyLoadedNotInitializedWhenUsingGetAsync()
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True);
}
}

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

Assert.That(book.Name, Is.EqualTo("some name"));
Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?"));
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
}
}
Expand All @@ -137,7 +148,6 @@ public async Task CanLoadAndSaveObjectInDifferentSessionsAsync()
}
}


[Test]
public async Task CanUpdateNonLazyWithoutLoadingLazyPropertyAsync()
{
Expand All @@ -147,17 +157,19 @@ public async Task CanUpdateNonLazyWithoutLoadingLazyPropertyAsync()
{
book = await (s.GetAsync<Book>(1));
book.Name += "updated";
book.FieldInterceptor += "updated";

Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "Before flush and commit");
await (trans.CommitAsync());
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "After flush and commit");
}


using (ISession s = OpenSession())
{
book = await (s.GetAsync<Book>(1));
Assert.That(book.Name, Is.EqualTo("some nameupdated"));
Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?updated"));
}
}
}
}
}
4 changes: 3 additions & 1 deletion src/NHibernate.Test/LazyProperty/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public virtual string ALotOfText
get { return _aLotOfText; }
set { _aLotOfText = value; }
}

public virtual string FieldInterceptor { get; set; }
}
}
}
22 changes: 17 additions & 5 deletions src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using NHibernate.Intercept;
using NHibernate.Tuple.Entity;
using NUnit.Framework;

Expand Down Expand Up @@ -31,14 +32,20 @@ protected override DebugSessionFactory BuildSessionFactory()

protected override void OnSetUp()
{
Assert.That(
nameof(Book.FieldInterceptor),
Is.EqualTo(nameof(IFieldInterceptorAccessor.FieldInterceptor)),
$"Test pre-condition not met: entity property {nameof(Book.FieldInterceptor)} should have the same " +
$"name than {nameof(IFieldInterceptorAccessor)}.{nameof(IFieldInterceptorAccessor.FieldInterceptor)}");
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Persist(new Book
{
Name = "some name",
Id = 1,
ALotOfText = "a lot of text ..."
ALotOfText = "a lot of text ...",
FieldInterceptor = "Why not that name?"
});
tx.Commit();
}
Expand All @@ -65,12 +72,14 @@ public void PropertyLoadedNotInitialized()
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.False);

NHibernateUtil.Initialize(book);

Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True);
}
}

Expand All @@ -90,6 +99,7 @@ public void PropertyLoadedNotInitializedWhenUsingGet()
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True);
}
}

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

Assert.That(book.Name, Is.EqualTo("some name"));
Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?"));
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
}
}
Expand All @@ -132,7 +143,6 @@ public void CanLoadAndSaveObjectInDifferentSessions()
}
}


[Test]
public void CanUpdateNonLazyWithoutLoadingLazyProperty()
{
Expand All @@ -142,17 +152,19 @@ public void CanUpdateNonLazyWithoutLoadingLazyProperty()
{
book = s.Get<Book>(1);
book.Name += "updated";
book.FieldInterceptor += "updated";

Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "Before flush and commit");
trans.Commit();
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "After flush and commit");
}


using (ISession s = OpenSession())
{
book = s.Get<Book>(1);
Assert.That(book.Name, Is.EqualTo("some nameupdated"));
Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?updated"));
}
}
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/LazyProperty/Mappings.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</id>
<property name="Name" />
<property name="ALotOfText" lazy="true" />
<property name="FieldInterceptor" />
</class>

</hibernate-mapping>
16 changes: 13 additions & 3 deletions src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public object Intercept(InvocationInfo info)
{
if (ReflectHelper.IsPropertyGet(info.TargetMethod))
{
if("get_FieldInterceptor".Equals(methodName))
if (IsGetFieldInterceptorCall(methodName, info.TargetMethod))
{
return FieldInterceptor;
}
Expand All @@ -33,7 +33,7 @@ public object Intercept(InvocationInfo info)
}
else if (ReflectHelper.IsPropertySet(info.TargetMethod))
{
if ("set_FieldInterceptor".Equals(methodName))
if (IsSetFieldInterceptorCall(methodName, info.TargetMethod))
{
FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
return null;
Expand All @@ -44,7 +44,7 @@ public object Intercept(InvocationInfo info)
}
else
{
if ("set_FieldInterceptor".Equals(methodName))
if (IsSetFieldInterceptorCall(methodName, info.TargetMethod))
{
FieldInterceptor = (IFieldInterceptor)info.Arguments[0];
return null;
Expand All @@ -62,5 +62,15 @@ public object Intercept(InvocationInfo info)
}
return returnValue;
}

private static bool IsGetFieldInterceptorCall(string methodName, MethodInfo targetMethod)
{
return "get_FieldInterceptor".Equals(methodName) && targetMethod.DeclaringType == typeof(IFieldInterceptorAccessor);
}

private static bool IsSetFieldInterceptorCall(string methodName, MethodInfo targetMethod)
{
return "set_FieldInterceptor".Equals(methodName) && targetMethod.DeclaringType == typeof(IFieldInterceptorAccessor);
}
}
}