Skip to content

Commit a840460

Browse files
committed
Merge branch 'NH-3795-4.0.x' into 4.0.x
2 parents 5c739ad + 18ac21e commit a840460

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System.Collections;
2+
using NHibernate.DomainModel;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3795
6+
{
7+
/// <summary>
8+
/// Tests in this class only failed when the code was build with the Roslyn compiler which is included in Visual Studio 2015
9+
/// </summary>
10+
[TestFixture]
11+
public class Fixture : TestCase
12+
{
13+
protected Child childAliasField = null;
14+
protected A aAliasField = null;
15+
16+
protected override IList Mappings
17+
{
18+
get { return new[] {"ParentChild.hbm.xml", "ABC.hbm.xml"}; }
19+
}
20+
21+
[Test]
22+
public void TestFieldAliasInQueryOver()
23+
{
24+
using (var s = sessions.OpenSession())
25+
{
26+
A rowalias = null;
27+
s.QueryOver(() => aAliasField)
28+
.SelectList(list => list
29+
.Select(() => aAliasField.Id).WithAlias(() => rowalias.Id))
30+
.List();
31+
}
32+
}
33+
34+
[Test]
35+
public void TestFieldAliasInQueryOverWithConversion()
36+
{
37+
using (var s = sessions.OpenSession())
38+
{
39+
B rowalias = null;
40+
s.QueryOver(() => aAliasField)
41+
.SelectList(list => list
42+
.Select(() => ((B)aAliasField).Count).WithAlias(() => rowalias.Count))
43+
.List();
44+
}
45+
}
46+
47+
[Test]
48+
public void TestFieldAliasInJoinAlias()
49+
{
50+
using (var s = sessions.OpenSession())
51+
{
52+
Child rowalias = null;
53+
s.QueryOver<Parent>()
54+
.JoinAlias(p => p.Child, () => childAliasField)
55+
.SelectList(list => list
56+
.Select(() => childAliasField.Id).WithAlias(() => rowalias.Id))
57+
.List();
58+
}
59+
}
60+
61+
[Test]
62+
public void TestFieldAliasInJoinQueryOver()
63+
{
64+
using (var s = sessions.OpenSession())
65+
{
66+
Child rowalias = null;
67+
s.QueryOver<Parent>()
68+
.JoinQueryOver(p => p.Child, () => childAliasField)
69+
.SelectList(list => list
70+
.Select(() => childAliasField.Id).WithAlias(() => rowalias.Id))
71+
.List();
72+
}
73+
}
74+
75+
[Test]
76+
public void TestAliasInQueryOver()
77+
{
78+
Child childAlias = null;
79+
A aAlias = null;
80+
using (var s = sessions.OpenSession())
81+
{
82+
A rowalias = null;
83+
s.QueryOver(() => aAlias)
84+
.SelectList(list => list
85+
.Select(() => aAlias.Id).WithAlias(() => rowalias.Id))
86+
.List();
87+
}
88+
}
89+
90+
[Test]
91+
public void TestAliasInQueryOverWithConversion()
92+
{
93+
Child childAlias = null;
94+
A aAlias = null;
95+
using (var s = sessions.OpenSession())
96+
{
97+
B rowalias = null;
98+
s.QueryOver(() => aAlias)
99+
.SelectList(list => list
100+
.Select(() => ((B) aAlias).Count).WithAlias(() => rowalias.Count))
101+
.List();
102+
}
103+
}
104+
105+
[Test]
106+
public void TestAliasInJoinAlias()
107+
{
108+
Child childAlias = null;
109+
A aAlias = null;
110+
using (var s = sessions.OpenSession())
111+
{
112+
Child rowalias = null;
113+
s.QueryOver<Parent>()
114+
.JoinAlias(p => p.Child, () => childAlias)
115+
.SelectList(list => list
116+
.Select(() => childAlias.Id).WithAlias(() => rowalias.Id))
117+
.List();
118+
}
119+
}
120+
121+
[Test]
122+
public void TestAliasInJoinQueryOver()
123+
{
124+
Child childAlias = null;
125+
A aAlias = null;
126+
using (var s = sessions.OpenSession())
127+
{
128+
Child rowalias = null;
129+
s.QueryOver<Parent>()
130+
.JoinQueryOver(p => p.Child, () => childAlias)
131+
.SelectList(list => list
132+
.Select(() => childAlias.Id).WithAlias(() => rowalias.Id))
133+
.List();
134+
}
135+
}
136+
}
137+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@
810810
<Compile Include="NHSpecificTest\NH3202\Fixture.cs" />
811811
<Compile Include="NHSpecificTest\NH3604\Entity.cs" />
812812
<Compile Include="NHSpecificTest\NH3604\FixtureByCode.cs" />
813+
<Compile Include="NHSpecificTest\NH3795\Fixture.cs" />
813814
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
814815
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />
815816
<Compile Include="NHSpecificTest\NH3234\Fixture.cs" />

src/NHibernate/Impl/ExpressionProcessor.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using System.Linq;
55
using System.Linq.Expressions;
66
using System.Reflection;
7-
7+
using System.Runtime.CompilerServices;
8+
using System.Text.RegularExpressions;
89
using NHibernate.Criterion;
910
using NHibernate.Util;
1011
using Expression = System.Linq.Expressions.Expression;
@@ -290,6 +291,21 @@ public static ProjectionInfo FindMemberProjection(Expression expression)
290291
return ProjectionInfo.ForProperty(FindMemberExpression(expression));
291292
}
292293

294+
//http://stackoverflow.com/a/2509524/259946
295+
private static readonly Regex GeneratedMemberNameRegex = new Regex(@"^(CS\$)?<\w*>[1-9a-s]__[a-zA-Z]+[0-9]*$", RegexOptions.Compiled | RegexOptions.Singleline);
296+
297+
private static bool IsCompilerGeneratedMemberExpressionOfCompilerGeneratedClass(Expression expression)
298+
{
299+
var memberExpression = expression as MemberExpression;
300+
if (memberExpression != null && memberExpression.Member.DeclaringType != null)
301+
{
302+
return Attribute.GetCustomAttribute(memberExpression.Member.DeclaringType, typeof(CompilerGeneratedAttribute)) != null
303+
&& GeneratedMemberNameRegex.IsMatch(memberExpression.Member.Name);
304+
}
305+
306+
return false;
307+
}
308+
293309
/// <summary>
294310
/// Retrieves the name of the property from a member expression
295311
/// </summary>
@@ -311,6 +327,11 @@ public static string FindMemberExpression(Expression expression)
311327
return FindMemberExpression(memberExpression.Expression);
312328
}
313329

330+
if (IsCompilerGeneratedMemberExpressionOfCompilerGeneratedClass(memberExpression.Expression))
331+
{
332+
return memberExpression.Member.Name;
333+
}
334+
314335
return FindMemberExpression(memberExpression.Expression) + "." + memberExpression.Member.Name;
315336
}
316337
if (IsConversion(memberExpression.Expression.NodeType))

0 commit comments

Comments
 (0)