Skip to content

Commit 45e8324

Browse files
committed
Implementing lazy properties
* Removing ignore from the lazy property fixture * Adding LazyFieldInterceptor impl for Castle. Adding IFieldInterceptorAccessor to allow setting the field interceptor Adding convenience methods to detect property invocations * Adding DefaultFieldInterceptor - since we don't need one tied to the actual field interception impl * Removing temporary #region * Detecting usage of non collection lazy properties in entity meta model and considering this for instrumented fields * Implementing FieldInterceptionHelper Delegating responsibility for detecting interception to the proxy factory * Adding support for detecting lazy properties in the mapping * Simplifying code * Removed overly complex code * Binding properties laziness * Adding the ability to create field interception proxy to proxy factories. * Will create field interception proxy when needed LazyPropertyFixture basic test now works * Fixing off by one error * Adding more tests to verify lazy properties actually work * Implementing empty impl of the new field interception capabilities to other proxy factories impl * Updating loaded sln projects SVN: trunk@4923
1 parent ddba860 commit 45e8324

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1050
-754
lines changed

src/NHibernate.ByteCode.Castle.Tests/ProxyInterface/CustomProxyFixture.cs

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public IProxyValidator ProxyValidator
7171
get { return new DynProxyTypeValidator(); }
7272
}
7373

74+
public bool IsInstrumented(System.Type entityClass)
75+
{
76+
return false;
77+
}
78+
7479
#endregion
7580
}
7681

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Castle.Core.Interceptor;
2+
using NHibernate.Intercept;
3+
using NHibernate.Util;
4+
5+
namespace NHibernate.ByteCode.Castle
6+
{
7+
public class LazyFieldInterceptor : IFieldInterceptorAccessor, global::Castle.Core.Interceptor.IInterceptor
8+
{
9+
public IFieldInterceptor FieldInterceptor
10+
{
11+
get;
12+
set;
13+
}
14+
15+
public void Intercept(IInvocation invocation)
16+
{
17+
if (FieldInterceptor != null)
18+
{
19+
if (ReflectHelper.IsPropertyGet(invocation.Method))
20+
{
21+
var result = FieldInterceptor.Intercept(invocation.InvocationTarget, ReflectHelper.GetPropertyName(invocation.Method));
22+
if (result == AbstractFieldInterceptor.InvokeImplementation)
23+
{
24+
invocation.Proceed();
25+
}
26+
else
27+
{
28+
invocation.ReturnValue = result;
29+
}
30+
}
31+
else if (ReflectHelper.IsPropertySet(invocation.Method))
32+
{
33+
FieldInterceptor.MarkDirty();
34+
FieldInterceptor.Intercept(invocation.InvocationTarget, ReflectHelper.GetPropertyName(invocation.Method));
35+
invocation.Proceed();
36+
}
37+
}
38+
else
39+
{
40+
invocation.Proceed();
41+
}
42+
}
43+
}
44+
}

src/NHibernate.ByteCode.Castle/NHibernate.ByteCode.Castle.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</ItemGroup>
5454
<ItemGroup>
5555
<Compile Include="AssemblyInfo.cs" />
56+
<Compile Include="LazyFieldInterceptor.cs" />
5657
<Compile Include="LazyInitializer.cs" />
5758
<Compile Include="ProxyFactory.cs" />
5859
<Compile Include="ProxyFactoryFactory.cs" />

src/NHibernate.ByteCode.Castle/ProxyFactory.cs

+9
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,14 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session
4343
throw new HibernateException("Creating a proxy instance failed", e);
4444
}
4545
}
46+
47+
48+
public override object GetFieldInterceptionProxy()
49+
{
50+
var proxyGenerationOptions = new ProxyGenerationOptions();
51+
var interceptor = new LazyFieldInterceptor();
52+
proxyGenerationOptions.AddMixinInstance(interceptor);
53+
return ProxyGenerator.CreateClassProxy(PersistentClass, proxyGenerationOptions, interceptor);
54+
}
4655
}
4756
}

src/NHibernate.ByteCode.Castle/ProxyFactoryFactory.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NHibernate.Bytecode;
23
using NHibernate.Proxy;
34

@@ -17,6 +18,11 @@ public IProxyValidator ProxyValidator
1718
get { return new DynProxyTypeValidator(); }
1819
}
1920

21+
public bool IsInstrumented(System.Type entityClass)
22+
{
23+
return true;
24+
}
25+
2026
#endregion
2127
}
2228
}

src/NHibernate.ByteCode.LinFu/ProxyFactoryFactory.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NHibernate.Bytecode;
23
using NHibernate.Proxy;
34

@@ -18,6 +19,11 @@ public IProxyValidator ProxyValidator
1819
get { return new DynProxyTypeValidator(); }
1920
}
2021

22+
public bool IsInstrumented(System.Type entityClass)
23+
{
24+
return false;
25+
}
26+
2127
#endregion
2228
}
2329
}

src/NHibernate.ByteCode.Spring/ProxyFactoryFactory.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NHibernate.Bytecode;
23
using NHibernate.Proxy;
34

@@ -21,6 +22,11 @@ public IProxyValidator ProxyValidator
2122
get { return new DynProxyTypeValidator(); }
2223
}
2324

25+
public bool IsInstrumented(System.Type entityClass)
26+
{
27+
return false;
28+
}
29+
2430
#endregion
2531
}
2632
}

src/NHibernate.Test/Bytecode/WrongProxyFactoryFactory.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NHibernate.Bytecode;
23
using NHibernate.Proxy;
34

@@ -22,6 +23,11 @@ public IProxyValidator ProxyValidator
2223
get { throw new System.NotImplementedException(); }
2324
}
2425

26+
public bool IsInstrumented(System.Type entityClass)
27+
{
28+
return false;
29+
}
30+
2531
#endregion
2632
}
2733
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections;
2+
using NHibernate.ByteCode.Castle;
3+
using NHibernate.Cfg;
24
using NUnit.Framework;
35

46
namespace NHibernate.Test.LazyProperty
@@ -13,27 +15,32 @@ protected override string MappingsAssembly
1315

1416
protected override IList Mappings
1517
{
16-
get { return new[] {"LazyProperty.Mappings.hbm.xml"}; }
18+
get { return new[] { "LazyProperty.Mappings.hbm.xml" }; }
1719
}
1820

19-
public void LoadData()
21+
protected override void Configure(NHibernate.Cfg.Configuration configuration)
2022
{
21-
Book b = new Book
22-
{
23-
Name = "some name",
24-
ALotOfText = "a lot of text ..."
25-
};
26-
27-
using(var s = OpenSession())
28-
using(var tx = s.BeginTransaction())
23+
configuration.SetProperty(Environment.ProxyFactoryFactoryClass,
24+
typeof(ProxyFactoryFactory).AssemblyQualifiedName);
25+
}
26+
27+
protected override void OnSetUp()
28+
{
29+
using (var s = OpenSession())
30+
using (var tx = s.BeginTransaction())
2931
{
30-
s.Persist(b);
32+
s.Persist(new Book
33+
{
34+
Name = "some name",
35+
Id = 1,
36+
ALotOfText = "a lot of text ..."
37+
});
3138
tx.Commit();
3239
}
33-
40+
3441
}
3542

36-
public void CleanUp()
43+
protected override void OnTearDown()
3744
{
3845
using (var s = OpenSession())
3946
using (var tx = s.BeginTransaction())
@@ -43,25 +50,61 @@ public void CleanUp()
4350
}
4451
}
4552

46-
[Test,Ignore("Not supported yet, waiting for a field-interceptor provider, probably Linfu.")]
53+
[Test]
4754
public void PropertyLoadedNotInitialized()
4855
{
49-
LoadData();
50-
51-
using(ISession s = OpenSession())
56+
using (ISession s = OpenSession())
5257
{
5358
var book = s.Load<Book>(1);
5459

5560
Assert.False(NHibernateUtil.IsPropertyInitialized(book, "Id"));
5661
Assert.False(NHibernateUtil.IsPropertyInitialized(book, "Name"));
5762
Assert.False(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"));
58-
var name = book.Name;
63+
64+
NHibernateUtil.Initialize(book);
65+
5966
Assert.True(NHibernateUtil.IsPropertyInitialized(book, "Id"));
6067
Assert.True(NHibernateUtil.IsPropertyInitialized(book, "Name"));
6168
Assert.False(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"));
6269
}
70+
}
71+
72+
[Test]
73+
public void PropertyLoadedNotInitializedWhenUsingGet()
74+
{
75+
using (ISession s = OpenSession())
76+
{
77+
var book = s.Get<Book>(1);
6378

64-
CleanUp();
79+
Assert.True(NHibernateUtil.IsPropertyInitialized(book, "Id"));
80+
Assert.True(NHibernateUtil.IsPropertyInitialized(book, "Name"));
81+
Assert.False(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"));
82+
}
83+
}
84+
85+
[Test]
86+
public void CanGetValueForLazyProperty()
87+
{
88+
using (ISession s = OpenSession())
89+
{
90+
var book = s.Get<Book>(1);
91+
92+
Assert.AreEqual("a lot of text ...", book.ALotOfText);
93+
Assert.True(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"));
94+
}
6595
}
96+
97+
[Test]
98+
public void CanGetValueForNonLazyProperty()
99+
{
100+
using (ISession s = OpenSession())
101+
{
102+
var book = s.Get<Book>(1);
103+
104+
Assert.AreEqual("some name", book.Name);
105+
Assert.False(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"));
106+
}
107+
}
108+
66109
}
67110
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<class name="Book">
77
<id name="Id">
8-
<generator class="native" />
8+
<generator class="assigned" />
99
</id>
1010
<property name="Name" />
1111
<property name="ALotOfText" lazy="true" />

src/NHibernate.Test/NHSpecificTest/SetFixture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public bool IsLazy
129129
{
130130
get
131131
{
132-
// TODO: Add CollectionPersisterStub.IsLazy getter implementation
132+
// TODO: Add CollectionPersisterStub.IsLazyProperty getter implementation
133133
return false;
134134
}
135135
}

src/NHibernate.Test/NHibernate.Test.build

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<include name="System.Data.OracleClient.dll" />
1818
<include name="Iesi.Collections.dll" />
1919
<include name="log4net.dll" />
20+
<include name="NHibernate.ByteCode.Castle.dll"/>
2021
<include name="NHibernate.ByteCode.LinFu.dll"/>
2122
<include name="NHibernate.DomainModel.dll" />
2223
<include name="NHibernate.dll" />

src/NHibernate.Test/NHibernate.Test.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,10 @@
16061606
<None Include="NHibernate.Test.nunit" />
16071607
</ItemGroup>
16081608
<ItemGroup>
1609+
<ProjectReference Include="..\NHibernate.ByteCode.Castle\NHibernate.ByteCode.Castle.csproj">
1610+
<Project>{31C3F0EA-0FED-4A2F-B68D-96CE29844487}</Project>
1611+
<Name>NHibernate.ByteCode.Castle</Name>
1612+
</ProjectReference>
16091613
<ProjectReference Include="..\NHibernate.ByteCode.LinFu\NHibernate.ByteCode.LinFu.csproj">
16101614
<Project>{8289D6AD-9714-42D3-A94D-D4D9814D1281}</Project>
16111615
<Name>NHibernate.ByteCode.LinFu</Name>

src/NHibernate.sln

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NHibernate.ByteCode.LinFu",
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NHibernate.ByteCode.LinFu.Tests", "NHibernate.ByteCode.LinFu.Tests\NHibernate.ByteCode.LinFu.Tests.csproj", "{94FDD99B-8275-4E51-8F43-958B2C632120}"
1818
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NHibernate.ByteCode.Castle", "NHibernate.ByteCode.Castle\NHibernate.ByteCode.Castle.csproj", "{31C3F0EA-0FED-4A2F-B68D-96CE29844487}"
20+
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NHibernate.ByteCode.Spring", "NHibernate.ByteCode.Spring\NHibernate.ByteCode.Spring.csproj", "{946BCA10-109A-4472-8521-76616174AD4D}"
22+
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NHibernate.ByteCode.Castle.Tests", "NHibernate.ByteCode.Castle.Tests\NHibernate.ByteCode.Castle.Tests.csproj", "{4972EE96-2417-4D47-9FF1-3B1D6B1D3191}"
24+
EndProject
1925
Global
2026
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2127
Debug|Any CPU = Debug|Any CPU
@@ -42,6 +48,18 @@ Global
4248
{94FDD99B-8275-4E51-8F43-958B2C632120}.Debug|Any CPU.Build.0 = Debug|Any CPU
4349
{94FDD99B-8275-4E51-8F43-958B2C632120}.Release|Any CPU.ActiveCfg = Release|Any CPU
4450
{94FDD99B-8275-4E51-8F43-958B2C632120}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{31C3F0EA-0FED-4A2F-B68D-96CE29844487}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52+
{31C3F0EA-0FED-4A2F-B68D-96CE29844487}.Debug|Any CPU.Build.0 = Debug|Any CPU
53+
{31C3F0EA-0FED-4A2F-B68D-96CE29844487}.Release|Any CPU.ActiveCfg = Release|Any CPU
54+
{31C3F0EA-0FED-4A2F-B68D-96CE29844487}.Release|Any CPU.Build.0 = Release|Any CPU
55+
{946BCA10-109A-4472-8521-76616174AD4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
56+
{946BCA10-109A-4472-8521-76616174AD4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
57+
{946BCA10-109A-4472-8521-76616174AD4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
58+
{946BCA10-109A-4472-8521-76616174AD4D}.Release|Any CPU.Build.0 = Release|Any CPU
59+
{4972EE96-2417-4D47-9FF1-3B1D6B1D3191}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
60+
{4972EE96-2417-4D47-9FF1-3B1D6B1D3191}.Debug|Any CPU.Build.0 = Debug|Any CPU
61+
{4972EE96-2417-4D47-9FF1-3B1D6B1D3191}.Release|Any CPU.ActiveCfg = Release|Any CPU
62+
{4972EE96-2417-4D47-9FF1-3B1D6B1D3191}.Release|Any CPU.Build.0 = Release|Any CPU
4563
EndGlobalSection
4664
GlobalSection(SolutionProperties) = preSolution
4765
HideSolutionNode = FALSE

src/NHibernate/Bytecode/IProxyFactoryFactory.cs

+2
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ public interface IProxyFactoryFactory
3636
*/
3737

3838
IProxyValidator ProxyValidator { get; }
39+
40+
bool IsInstrumented(System.Type entityClass);
3941
}
4042
}

src/NHibernate/Cfg/MappingSchema/HbmAny.cs

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public bool OptimisticLock
2222
get { return optimisticlock; }
2323
}
2424

25+
public bool IsLazyProperty
26+
{
27+
get { return lazy; }
28+
}
29+
2530
#endregion
2631

2732
#region Overrides of AbstractDecoratable

src/NHibernate/Cfg/MappingSchema/HbmArray.cs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public string Access
1616
get { return access; }
1717
}
1818

19+
public bool IsLazyProperty
20+
{
21+
get { return false; }
22+
}
23+
1924
public bool OptimisticLock
2025
{
2126
get { return optimisticlock; }

src/NHibernate/Cfg/MappingSchema/HbmBag.cs

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public string Access
1717
get { return access; }
1818
}
1919

20+
public bool IsLazyProperty
21+
{
22+
get { return false; }
23+
}
24+
2025
public bool OptimisticLock
2126
{
2227
get { return optimisticlock; }

0 commit comments

Comments
 (0)