Skip to content

Commit 0c761e6

Browse files
committed
Refactoring of contributed code
1 parent 8b22343 commit 0c761e6

File tree

4 files changed

+23
-37
lines changed

4 files changed

+23
-37
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1+
using System.Linq;
12
using NHibernate.Linq;
23
using NUnit.Framework;
34

45
namespace NHibernate.Test.NHSpecificTest.NH3408
56
{
6-
using System.Linq;
7-
87
public class Fixture : BugTestCase
98
{
109
[Test]
1110
public void ProjectAnonymousTypeWithArrayProperty()
1211
{
1312
using (var session = OpenSession())
13+
using (session.BeginTransaction())
1414
{
1515
var query = from c in session.Query<Country>()
1616
select new { c.Picture, c.NationalHolidays };
1717

18-
var result = query.ToList();
18+
Assert.DoesNotThrow(() => { query.ToList(); });
1919
}
2020
}
2121
}
22-
}
22+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
using System;
2+
13
namespace NHibernate.Test.NHSpecificTest.NH3408
24
{
3-
using System;
4-
55
public class Country
66
{
77
public virtual int Id { get; set; }
@@ -12,4 +12,4 @@ public class Country
1212

1313
public virtual DateTime[] NationalHolidays { get; set; }
1414
}
15-
}
15+
}

src/NHibernate/Linq/NestedSelects/NestedSelectDetector.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
using System.Collections.Generic;
22
using System.Linq.Expressions;
3+
using System.Reflection;
34
using NHibernate.Linq.Visitors;
45
using Remotion.Linq.Clauses.Expressions;
56

67
namespace NHibernate.Linq.NestedSelects
78
{
8-
using System.Reflection;
9-
using NHibernate.Metadata;
10-
119
internal class NestedSelectDetector : NhExpressionTreeVisitor
1210
{
11+
private readonly ISessionFactory sessionFactory;
1312
private readonly ICollection<Expression> _expressions = new List<Expression>();
14-
private readonly IDictionary<string, ICollectionMetadata> _collectionMetadata;
1513

16-
public NestedSelectDetector(IDictionary<string, ICollectionMetadata> collectionMetadata)
14+
public NestedSelectDetector(ISessionFactory sessionFactory)
1715
{
18-
_collectionMetadata = collectionMetadata;
16+
this.sessionFactory = sessionFactory;
1917
}
2018

2119
public ICollection<Expression> Expressions
@@ -53,9 +51,9 @@ protected override Expression VisitMemberExpression(MemberExpression expression)
5351

5452
private bool IsMappedCollection(MemberInfo memberInfo)
5553
{
56-
var key = memberInfo.DeclaringType.FullName + "." + memberInfo.Name;
54+
var collectionRole = memberInfo.DeclaringType.FullName + "." + memberInfo.Name;
5755

58-
return _collectionMetadata.ContainsKey(key);
56+
return sessionFactory.GetCollectionMetadata(collectionRole) != null;
5957
}
6058
}
61-
}
59+
}

src/NHibernate/Linq/NestedSelects/NestedSelectRewriter.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using NHibernate.Linq.Clauses;
99
using NHibernate.Linq.GroupBy;
1010
using NHibernate.Linq.Visitors;
11+
using NHibernate.Util;
1112
using Remotion.Linq;
1213
using Remotion.Linq.Clauses.Expressions;
1314

@@ -19,7 +20,7 @@ internal static class NestedSelectRewriter
1920

2021
public static void ReWrite(QueryModel queryModel, ISessionFactory sessionFactory)
2122
{
22-
var nsqmv = new NestedSelectDetector(sessionFactory.GetAllCollectionMetadata());
23+
var nsqmv = new NestedSelectDetector(sessionFactory);
2324
nsqmv.VisitExpression(queryModel.SelectClause.Selector);
2425
if (!nsqmv.HasSubqueries)
2526
return;
@@ -113,8 +114,8 @@ private static Expression ProcessSubquery(ISessionFactory sessionFactory, IColle
113114
private static Expression ProcessMemberExpression(ISessionFactory sessionFactory, ICollection<ExpressionHolder> elementExpression, QueryModel queryModel, Expression @group, Expression memberExpression)
114115
{
115116
var join = new NhJoinClause(new NameGenerator(queryModel).GetNewName(),
116-
GetElementType((MemberExpression) memberExpression),
117-
memberExpression);
117+
GetElementType(memberExpression.Type),
118+
memberExpression);
118119

119120
queryModel.BodyClauses.Add(@join);
120121

@@ -248,25 +249,12 @@ private static Expression ConvertToObject(Expression expression)
248249
return Expression.Convert(expression, typeof(object));
249250
}
250251

251-
private static System.Type GetElementType(MemberExpression expression)
252+
private static System.Type GetElementType(System.Type type)
252253
{
253-
var type = expression.Type;
254-
if (!type.IsCollectionType())
255-
{
256-
throw new ArgumentException();
257-
}
258-
259-
if (type.IsGenericType)
260-
{
261-
return type.GetGenericArguments()[0];
262-
}
263-
264-
if (type.IsArray)
265-
{
266-
return type.GetElementType();
267-
}
268-
269-
throw new NotSupportedException("Unknown collection type " + type.FullName);
254+
var elementType = ReflectHelper.GetCollectionElementType(type);
255+
if (elementType == null)
256+
throw new NotSupportedException("Unknown collection type " + type.FullName);
257+
return elementType;
270258
}
271259
}
272260
}

0 commit comments

Comments
 (0)