Skip to content

Commit 5e9e273

Browse files
committed
Merge branch 'ngbrown-NH-3604'
2 parents e819f17 + af4495c commit 5e9e273

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3604
5+
{
6+
public class Entity
7+
{
8+
protected virtual Guid Id { get; set; }
9+
public virtual string Name { get; set; }
10+
public virtual EntityDetail Detail { get; set; }
11+
12+
public static class PropertyAccessExpressions
13+
{
14+
public static readonly Expression<Func<Entity, Guid>> Id = x => x.Id;
15+
}
16+
}
17+
18+
public class EntityDetail
19+
{
20+
// Required by NHibernate
21+
protected EntityDetail()
22+
{
23+
}
24+
25+
public EntityDetail(Entity entity)
26+
{
27+
this.Entity = entity;
28+
}
29+
30+
public virtual Guid Id { get; set; }
31+
protected virtual Entity Entity { get; set; }
32+
public virtual string ExtraInfo { get; set; }
33+
34+
public static class PropertyAccessExpressions
35+
{
36+
public static readonly Expression<Func<EntityDetail, Entity>> Entity = x => x.Entity;
37+
}
38+
}
39+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Linq;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.NH3604
8+
{
9+
/// <summary>
10+
/// Tests ability to map a non-public property by code via expressions to access the hidden properties
11+
/// </summary>
12+
public class ByCodeFixture : TestCaseMappingByCode
13+
{
14+
protected override HbmMapping GetMappings()
15+
{
16+
var mapper = new ModelMapper();
17+
mapper.Class<Entity>(rc =>
18+
{
19+
rc.Id(Entity.PropertyAccessExpressions.Id, m => m.Generator(Generators.GuidComb));
20+
rc.Property(x => x.Name);
21+
rc.OneToOne(x => x.Detail, m => m.Cascade(Mapping.ByCode.Cascade.All));
22+
});
23+
24+
mapper.Class<EntityDetail>(rc =>
25+
{
26+
rc.Id(x => x.Id, m => m.Generator(new ForeignGeneratorDef(ReflectionHelper.GetProperty(EntityDetail.PropertyAccessExpressions.Entity))));
27+
rc.OneToOne(EntityDetail.PropertyAccessExpressions.Entity, m => m.Constrained(true));
28+
rc.Property(x => x.ExtraInfo);
29+
});
30+
31+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
32+
}
33+
34+
protected override void OnSetUp()
35+
{
36+
using (ISession session = OpenSession())
37+
using (ITransaction transaction = session.BeginTransaction())
38+
{
39+
var e1 = new Entity { Name = "Bob" };
40+
session.Save(e1);
41+
42+
43+
var e2 = new Entity { Name = "Sally" };
44+
var ed2 = new EntityDetail(e2) { ExtraInfo = "Jo" };
45+
e2.Detail = ed2;
46+
47+
session.Save(e2);
48+
49+
session.Flush();
50+
transaction.Commit();
51+
}
52+
}
53+
54+
protected override void OnTearDown()
55+
{
56+
using (ISession session = OpenSession())
57+
using (ITransaction transaction = session.BeginTransaction())
58+
{
59+
session.Delete("from System.Object");
60+
61+
session.Flush();
62+
transaction.Commit();
63+
}
64+
}
65+
66+
[Test]
67+
public void CanPerformQueryOnMappedClassWithProtectedProperty()
68+
{
69+
using (ISession session = OpenSession())
70+
using (session.BeginTransaction())
71+
{
72+
var result = from e in session.Query<Entity>()
73+
where e.Name == "Sally"
74+
select e;
75+
76+
var entities = result.ToList();
77+
Assert.AreEqual(1, entities.Count);
78+
Assert.AreEqual("Jo", entities[0].Detail.ExtraInfo);
79+
}
80+
}
81+
82+
[Test]
83+
public void CanWriteMappingsReferencingProtectedProperty()
84+
{
85+
var mapper = new ModelMapper();
86+
mapper.Class<Entity>(rc =>
87+
{
88+
rc.Id(Entity.PropertyAccessExpressions.Id, m => m.Generator(Generators.GuidComb));
89+
rc.Property(x => x.Name);
90+
});
91+
mapper.CompileMappingForEachExplicitlyAddedEntity().WriteAllXmlMapping();
92+
}
93+
}
94+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@
792792
</Compile>
793793
<Compile Include="NHSpecificTest\NH3202\Domain.cs" />
794794
<Compile Include="NHSpecificTest\NH3202\Fixture.cs" />
795+
<Compile Include="NHSpecificTest\NH3604\Entity.cs" />
796+
<Compile Include="NHSpecificTest\NH3604\FixtureByCode.cs" />
795797
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
796798
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />
797799
<Compile Include="NHSpecificTest\NH3234\Fixture.cs" />

src/NHibernate/Mapping/ByCode/TypeExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public static MemberInfo DecodeMemberAccessExpressionOf<TEntity, TProperty>(Expr
100100
return memberOfDeclaringType;
101101
}
102102

103-
return typeof (TEntity).GetProperty(memberOfDeclaringType.Name, memberOfDeclaringType.GetPropertyOrFieldType());
103+
return typeof (TEntity).GetProperty(memberOfDeclaringType.Name, PropertiesOfClassHierarchy,
104+
null, memberOfDeclaringType.GetPropertyOrFieldType(), new System.Type[0], null);
104105
}
105106

106107
public static MemberInfo GetMemberFromDeclaringType(this MemberInfo source)

0 commit comments

Comments
 (0)