Skip to content

Commit 1b6a478

Browse files
committed
Adding error in the log about not using auto prop
SVN: trunk@4924
1 parent 45e8324 commit 1b6a478

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs

+19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
using System.Collections;
2+
using log4net.Core;
23
using NHibernate.ByteCode.Castle;
34
using NHibernate.Cfg;
5+
using NHibernate.Tuple.Entity;
46
using NUnit.Framework;
57

68
namespace NHibernate.Test.LazyProperty
79
{
810
[TestFixture]
911
public class LazyPropertyFixture : TestCase
1012
{
13+
private string log;
14+
1115
protected override string MappingsAssembly
1216
{
1317
get { return "NHibernate.Test"; }
@@ -24,6 +28,15 @@ protected override void Configure(NHibernate.Cfg.Configuration configuration)
2428
typeof(ProxyFactoryFactory).AssemblyQualifiedName);
2529
}
2630

31+
protected override void BuildSessionFactory()
32+
{
33+
using (var logSpy = new LogSpy(typeof(EntityMetamodel)))
34+
{
35+
base.BuildSessionFactory();
36+
log = logSpy.GetWholeLog();
37+
}
38+
}
39+
2740
protected override void OnSetUp()
2841
{
2942
using (var s = OpenSession())
@@ -69,6 +82,12 @@ public void PropertyLoadedNotInitialized()
6982
}
7083
}
7184

85+
[Test]
86+
public void ShouldGenerateErrorForNonAutoPropLazyProp()
87+
{
88+
Assert.IsTrue(log.Contains("Lazy property NHibernate.Test.LazyProperty.Book.ALotOfText is not an auto property, which may result in uninitialized property access"));
89+
}
90+
7291
[Test]
7392
public void PropertyLoadedNotInitializedWhenUsingGet()
7493
{

src/NHibernate/Tuple/Entity/EntityMetamodel.cs

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
34
using Iesi.Collections.Generic;
45
using log4net;
56
using NHibernate.Engine;
@@ -229,6 +230,18 @@ public EntityMetamodel(PersistentClass persistentClass, ISessionFactoryImplement
229230
else
230231
{
231232
log.Info("lazy property fetching available for: " + name);
233+
foreach (var prop in persistentClass.PropertyClosureIterator)
234+
{
235+
if (prop.IsLazy == false)
236+
continue;
237+
238+
var getter = prop.GetGetter(persistentClass.MappedClass);
239+
if(getter.Method == null ||
240+
getter.Method.IsDefined(typeof(CompilerGeneratedAttribute), false) == false)
241+
{
242+
log.ErrorFormat("Lazy property {0}.{1} is not an auto property, which may result in uninitialized property access", persistentClass.EntityName, prop.Name);
243+
}
244+
}
232245
}
233246
}
234247

0 commit comments

Comments
 (0)