Skip to content

Commit 66e6db7

Browse files
committed
Code cleanup
1 parent b37ffde commit 66e6db7

File tree

7 files changed

+172
-156
lines changed

7 files changed

+172
-156
lines changed
Lines changed: 48 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,30 @@
11
using System;
2-
using Remotion.Linq.Clauses;
3-
using Remotion.Linq.Clauses.ExpressionVisitors;
42
using System.Linq.Expressions;
53
using Remotion.Linq;
4+
using Remotion.Linq.Clauses;
65

76
namespace NHibernate.Linq.Clauses
87
{
9-
public class NhHavingClause : NhWhereClause, IBodyClause, IClause
8+
public class NhHavingClause : IBodyClause
109
{
11-
public NhHavingClause(Expression predicate)
12-
: base(predicate)
13-
{
14-
}
15-
16-
public override string ToString()
17-
{
18-
return "having " + Predicate;
19-
}
10+
Expression _predicate;
2011

2112
/// <summary>
22-
/// Accepts the specified visitor by calling its <see cref="M:Remotion.Linq.IQueryModelVisitor.VisitWhereClause(Remotion.Linq.Clauses.WhereClause,Remotion.Linq.QueryModel,System.Int32)" /> method.
13+
/// Initializes a new instance of the <see cref="T:Remotion.Linq.Clauses.WhereClause" /> class.
2314
/// </summary>
24-
/// <param name="visitor">The visitor to accept.</param>
25-
/// <param name="queryModel">The query model in whose context this clause is visited.</param>
26-
/// <param name="index">The index of this clause in the <paramref name="queryModel" />'s <see cref="P:Remotion.Linq.QueryModel.BodyClauses" /> collection.</param>
27-
public override void Accept(IQueryModelVisitor visitor, QueryModel queryModel, int index)
28-
{
29-
if (visitor == null) throw new ArgumentNullException(nameof(visitor));
30-
if (queryModel == null) throw new ArgumentNullException(nameof(queryModel));
31-
((INhQueryModelVisitor) visitor).VisitNhHavingClause(this, queryModel, index);
32-
}
33-
34-
IBodyClause IBodyClause.Clone(CloneContext cloneContext)
15+
/// <param name="predicate">The predicate used to filter data items.</param>
16+
public NhHavingClause(Expression predicate)
3517
{
36-
return Clone(cloneContext);
18+
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
19+
_predicate = predicate;
3720
}
38-
}
39-
40-
public abstract class NhWhereClause
41-
{
42-
private Expression _predicate;
4321

4422
/// <summary>
45-
/// Gets the predicate, the expression representing the where condition by which the data items are filtered
23+
/// Gets the predicate, the expression representing the where condition by which the data items are filtered
4624
/// </summary>
4725
public Expression Predicate
4826
{
49-
get
50-
{
51-
return _predicate;
52-
}
27+
get { return _predicate; }
5328
set
5429
{
5530
if (value == null) throw new ArgumentNullException(nameof(value));
@@ -58,47 +33,59 @@ public Expression Predicate
5833
}
5934

6035
/// <summary>
61-
/// Initializes a new instance of the <see cref="T:Remotion.Linq.Clauses.WhereClause" /> class.
36+
/// Accepts the specified visitor by calling its
37+
/// <see
38+
/// cref="M:Remotion.Linq.IQueryModelVisitor.VisitWhereClause(Remotion.Linq.Clauses.WhereClause,Remotion.Linq.QueryModel,System.Int32)" />
39+
/// method.
6240
/// </summary>
63-
/// <param name="predicate">The predicate used to filter data items.</param>
64-
public NhWhereClause(Expression predicate)
41+
/// <param name="visitor">The visitor to accept.</param>
42+
/// <param name="queryModel">The query model in whose context this clause is visited.</param>
43+
/// <param name="index">
44+
/// The index of this clause in the <paramref name="queryModel" />'s
45+
/// <see cref="P:Remotion.Linq.QueryModel.BodyClauses" /> collection.
46+
/// </param>
47+
public void Accept(IQueryModelVisitor visitor, QueryModel queryModel, int index)
6548
{
66-
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
67-
_predicate = predicate;
49+
if (visitor == null) throw new ArgumentNullException(nameof(visitor));
50+
if (queryModel == null) throw new ArgumentNullException(nameof(queryModel));
51+
((INhQueryModelVisitor) visitor).VisitNhHavingClause(this, queryModel, index);
6852
}
6953

70-
/// <summary>
71-
/// Accepts the specified visitor by calling its <see cref="M:Remotion.Linq.IQueryModelVisitor.VisitWhereClause(Remotion.Linq.Clauses.WhereClause,Remotion.Linq.QueryModel,System.Int32)" /> method.
72-
/// </summary>
73-
/// <param name="visitor">The visitor to accept.</param>
74-
/// <param name="queryModel">The query model in whose context this clause is visited.</param>
75-
/// <param name="index">The index of this clause in the <paramref name="queryModel" />'s <see cref="P:Remotion.Linq.QueryModel.BodyClauses" /> collection.</param>
76-
public abstract void Accept(IQueryModelVisitor visitor, QueryModel queryModel, int index);
54+
IBodyClause IBodyClause.Clone(CloneContext cloneContext)
55+
{
56+
return Clone(cloneContext);
57+
}
7758

7859
/// <summary>
79-
/// Transforms all the expressions in this clause and its child objects via the given <paramref name="transformation" /> delegate.
60+
/// Transforms all the expressions in this clause and its child objects via the given
61+
/// <paramref name="transformation" /> delegate.
8062
/// </summary>
81-
/// <param name="transformation">The transformation object. This delegate is called for each <see cref="T:System.Linq.Expressions.Expression" /> within this
82-
/// clause, and those expressions will be replaced with what the delegate returns.</param>
63+
/// <param name="transformation">
64+
/// The transformation object. This delegate is called for each <see cref="T:System.Linq.Expressions.Expression" />
65+
/// within this
66+
/// clause, and those expressions will be replaced with what the delegate returns.
67+
/// </param>
8368
public void TransformExpressions(Func<Expression, Expression> transformation)
8469
{
85-
if (transformation == null) throw new ArgumentNullException("transformation");
70+
if (transformation == null) throw new ArgumentNullException(nameof(transformation));
8671
Predicate = transformation(Predicate);
8772
}
8873

89-
/// <summary>Clones this clause.</summary>
90-
/// <param name="cloneContext">The clones of all query source clauses are registered with this <see cref="T:Remotion.Linq.Clauses.CloneContext" />.</param>
91-
/// <returns></returns>
92-
public WhereClause Clone(CloneContext cloneContext)
74+
public override string ToString()
9375
{
94-
if (cloneContext == null) throw new ArgumentNullException("cloneContext");
95-
return new WhereClause(Predicate);
76+
return "having " + Predicate;
9677
}
9778

98-
public override string ToString()
79+
/// <summary>Clones this clause.</summary>
80+
/// <param name="cloneContext">
81+
/// The clones of all query source clauses are registered with this
82+
/// <see cref="T:Remotion.Linq.Clauses.CloneContext" />.
83+
/// </param>
84+
/// <returns></returns>
85+
public NhHavingClause Clone(CloneContext cloneContext)
9986
{
100-
return "where " + Predicate;
87+
if (cloneContext == null) throw new ArgumentNullException(nameof(cloneContext));
88+
return new NhHavingClause(Predicate);
10189
}
10290
}
103-
104-
}
91+
}

src/NHibernate/Linq/Clauses/NhJoinClause.cs

Lines changed: 70 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,97 @@
1010
namespace NHibernate.Linq.Clauses
1111
{
1212
/// <summary>
13-
/// All joins are created as outer joins. An optimization in <see cref="WhereJoinDetector"/> finds
14-
/// joins that may be inner joined and calls <see cref="MakeInner"/> on them.
15-
/// <see cref="QueryModelVisitor"/>'s <see cref="QueryModelVisitor.VisitAdditionalFromClause"/> will
16-
/// then emit the correct HQL join.
13+
/// All joins are created as outer joins. An optimization in <see cref="WhereJoinDetector" /> finds
14+
/// joins that may be inner joined and calls <see cref="MakeInner" /> on them.
15+
/// <see cref="QueryModelVisitor" />'s <see cref="QueryModelVisitor.VisitAdditionalFromClause" /> will
16+
/// then emit the correct HQL join.
1717
/// </summary>
18-
public class NhJoinClause : NhFromClauseBase, IBodyClause, IClause
18+
public class NhJoinClause : IBodyClause, IFromClause
1919
{
20+
Expression _fromExpression;
21+
string _itemName;
22+
System.Type _itemType;
23+
2024
public NhJoinClause(string itemName, System.Type itemType, Expression fromExpression)
2125
: this(itemName, itemType, fromExpression, new NhWithClause[0])
2226
{
2327
}
2428

29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="T:Remotion.Linq.Clauses.NhJoinClause" /> class.
31+
/// </summary>
32+
/// <param name="itemName">A name describing the items generated by the from clause.</param>
33+
/// <param name="itemType">The type of the items generated by the from clause.</param>
34+
/// <param name="fromExpression">
35+
/// The <see cref="T:System.Linq.Expressions.Expression" /> generating data items for this
36+
/// from clause.
37+
/// </param>
38+
/// <param name="restrictions"></param>
2539
public NhJoinClause(string itemName, System.Type itemType, Expression fromExpression, IEnumerable<NhWithClause> restrictions)
26-
: base(itemName, itemType, fromExpression)
2740
{
28-
Restrictions = new ObservableCollection<NhWithClause>();
29-
foreach (var withClause in restrictions)
30-
Restrictions.Add(withClause);
41+
if (string.IsNullOrEmpty(itemName)) throw new ArgumentException("Value cannot be null or empty.", nameof(itemName));
42+
if (itemType == null) throw new ArgumentNullException(nameof(itemType));
43+
if (fromExpression == null) throw new ArgumentNullException(nameof(fromExpression));
44+
45+
_itemName = itemName;
46+
_itemType = itemType;
47+
_fromExpression = fromExpression;
48+
49+
Restrictions = new ObservableCollection<NhWithClause>(restrictions);
3150
IsInner = false;
3251
}
3352

34-
public ObservableCollection<NhWithClause> Restrictions { get; private set; }
53+
public ObservableCollection<NhWithClause> Restrictions { get; }
3554

3655
public bool IsInner { get; private set; }
3756

38-
public NhJoinClause Clone(CloneContext cloneContext)
39-
{
40-
var joinClause = new NhJoinClause(ItemName, ItemType, FromExpression);
41-
foreach (var withClause in Restrictions)
42-
{
43-
var withClause2 = new NhWithClause(withClause.Predicate);
44-
joinClause.Restrictions.Add(withClause2);
45-
}
46-
47-
cloneContext.QuerySourceMapping.AddMapping(this, new QuerySourceReferenceExpression(joinClause));
48-
return joinClause;
49-
}
50-
51-
public void MakeInner()
52-
{
53-
IsInner = true;
54-
}
55-
56-
public override void TransformExpressions(Func<Expression, Expression> transformation)
57+
public void TransformExpressions(Func<Expression, Expression> transformation)
5758
{
59+
if (transformation == null) throw new ArgumentNullException(nameof(transformation));
5860
foreach (var withClause in Restrictions)
5961
withClause.TransformExpressions(transformation);
60-
base.TransformExpressions(transformation);
62+
FromExpression = transformation(FromExpression);
6163
}
6264

6365
/// <summary>
64-
/// Accepts the specified visitor by calling its <see cref="M:Remotion.Linq.IQueryModelVisitor.VisitAdditionalFromClause(Remotion.Linq.Clauses.AdditionalFromClause,Remotion.Linq.QueryModel,System.Int32)" /> method.
66+
/// Accepts the specified visitor by calling its
67+
/// <see
68+
/// cref="M:Remotion.Linq.IQueryModelVisitor.VisitAdditionalFromClause(Remotion.Linq.Clauses.AdditionalFromClause,Remotion.Linq.QueryModel,System.Int32)" />
69+
/// method.
6570
/// </summary>
6671
/// <param name="visitor">The visitor to accept.</param>
6772
/// <param name="queryModel">The query model in whose context this clause is visited.</param>
68-
/// <param name="index">The index of this clause in the <paramref name="queryModel" />'s <see cref="P:Remotion.Linq.QueryModel.BodyClauses" /> collection.</param>
73+
/// <param name="index">
74+
/// The index of this clause in the <paramref name="queryModel" />'s
75+
/// <see cref="P:Remotion.Linq.QueryModel.BodyClauses" /> collection.
76+
/// </param>
6977
public void Accept(IQueryModelVisitor visitor, QueryModel queryModel, int index)
7078
{
7179
if (visitor == null) throw new ArgumentNullException(nameof(visitor));
7280
if (queryModel == null) throw new ArgumentNullException(nameof(queryModel));
7381
var nhVisitor = visitor as INhQueryModelVisitor;
74-
if (nhVisitor == null) throw new ArgumentException("Expect visitor to implement INhQueryModelVisitor", nameof(visitor));
82+
if (nhVisitor == null)
83+
throw new ArgumentException("Expect visitor to implement INhQueryModelVisitor", nameof(visitor));
7584
nhVisitor.VisitNhJoinClause(this, queryModel, index);
7685
}
7786

7887
IBodyClause IBodyClause.Clone(CloneContext cloneContext)
7988
{
8089
return Clone(cloneContext);
8190
}
82-
}
83-
84-
public abstract class NhFromClauseBase : IFromClause, IClause, IQuerySource
85-
{
86-
private string _itemName;
87-
private System.Type _itemType;
88-
private Expression _fromExpression;
8991

9092
/// <summary>
91-
/// Gets or sets a name describing the items generated by this from clause.
93+
/// Gets or sets a name describing the items generated by this from clause.
9294
/// </summary>
9395
/// <remarks>
94-
/// Item names are inferred when a query expression is parsed, and they usually correspond to the variable names present in that expression.
95-
/// However, note that names are not necessarily unique within a <see cref="T:Remotion.Linq.QueryModel" />. Use names only for readability and debugging, not for
96-
/// uniquely identifying <see cref="T:Remotion.Linq.Clauses.IQuerySource" /> objects. To match an <see cref="T:Remotion.Linq.Clauses.IQuerySource" /> with its references, use the
97-
/// <see cref="P:Remotion.Linq.Clauses.Expressions.QuerySourceReferenceExpression.ReferencedQuerySource" /> property rather than the <see cref="P:Remotion.Linq.Clauses.NhFromClauseBase.ItemName" />.
96+
/// Item names are inferred when a query expression is parsed, and they usually correspond to the variable names
97+
/// present in that expression.
98+
/// However, note that names are not necessarily unique within a <see cref="T:Remotion.Linq.QueryModel" />. Use names
99+
/// only for readability and debugging, not for
100+
/// uniquely identifying <see cref="T:Remotion.Linq.Clauses.IQuerySource" /> objects. To match an
101+
/// <see cref="T:Remotion.Linq.Clauses.IQuerySource" /> with its references, use the
102+
/// <see cref="P:Remotion.Linq.Clauses.Expressions.QuerySourceReferenceExpression.ReferencedQuerySource" /> property
103+
/// rather than the <see cref="P:Remotion.Linq.Clauses.NhFromClauseBase.ItemName" />.
98104
/// </remarks>
99105
public string ItemName
100106
{
@@ -107,11 +113,14 @@ public string ItemName
107113
}
108114

109115
/// <summary>
110-
/// Gets or sets the type of the items generated by this from clause.
116+
/// Gets or sets the type of the items generated by this from clause.
111117
/// </summary>
112118
/// <note type="warning">
113-
/// Changing the <see cref="P:Remotion.Linq.Clauses.NhFromClauseBase.ItemType" /> of a <see cref="T:Remotion.Linq.Clauses.IQuerySource" /> can make all <see cref="T:Remotion.Linq.Clauses.Expressions.QuerySourceReferenceExpression" /> objects that
114-
/// point to that <see cref="T:Remotion.Linq.Clauses.IQuerySource" /> invalid, so the property setter should be used with care.
119+
/// Changing the <see cref="P:Remotion.Linq.Clauses.NhFromClauseBase.ItemType" /> of a
120+
/// <see cref="T:Remotion.Linq.Clauses.IQuerySource" /> can make all
121+
/// <see cref="T:Remotion.Linq.Clauses.Expressions.QuerySourceReferenceExpression" /> objects that
122+
/// point to that <see cref="T:Remotion.Linq.Clauses.IQuerySource" /> invalid, so the property setter should be used
123+
/// with care.
115124
/// </note>
116125
public System.Type ItemType
117126
{
@@ -124,7 +133,7 @@ public System.Type ItemType
124133
}
125134

126135
/// <summary>
127-
/// The expression generating the data items for this from clause.
136+
/// The expression generating the data items for this from clause.
128137
/// </summary>
129138
public Expression FromExpression
130139
{
@@ -136,44 +145,29 @@ public Expression FromExpression
136145
}
137146
}
138147

139-
/// <summary>
140-
/// Initializes a new instance of the <see cref="T:Remotion.Linq.Clauses.NhFromClauseBase" /> class.
141-
/// </summary>
142-
/// <param name="itemName">A name describing the items generated by the from clause.</param>
143-
/// <param name="itemType">The type of the items generated by the from clause.</param>
144-
/// <param name="fromExpression">The <see cref="T:System.Linq.Expressions.Expression" /> generating data items for this from clause.</param>
145-
internal NhFromClauseBase(string itemName, System.Type itemType, Expression fromExpression)
146-
{
147-
if (string.IsNullOrEmpty(itemName)) throw new ArgumentException("Value cannot be null or empty.", nameof(itemName));
148-
if (itemType == null) throw new ArgumentNullException(nameof(itemType));
149-
if (fromExpression == null) throw new ArgumentNullException(nameof(fromExpression));
150-
_itemName = itemName;
151-
_itemType = itemType;
152-
_fromExpression = fromExpression;
153-
}
154-
155-
public virtual void CopyFromSource(IFromClause source)
148+
public void CopyFromSource(IFromClause source)
156149
{
157150
if (source == null) throw new ArgumentNullException(nameof(source));
158151
FromExpression = source.FromExpression;
159152
ItemName = source.ItemName;
160153
ItemType = source.ItemType;
161154
}
162155

163-
/// <summary>
164-
/// Transforms all the expressions in this clause and its child objects via the given <paramref name="transformation" /> delegate.
165-
/// </summary>
166-
/// <param name="transformation">The transformation object. This delegate is called for each <see cref="T:System.Linq.Expressions.Expression" /> within this
167-
/// clause, and those expressions will be replaced with what the delegate returns.</param>
168-
public virtual void TransformExpressions(Func<Expression, Expression> transformation)
156+
public NhJoinClause Clone(CloneContext cloneContext)
169157
{
170-
if (transformation == null) throw new ArgumentNullException(nameof(transformation));
171-
FromExpression = transformation(FromExpression);
158+
var joinClause = new NhJoinClause(ItemName, ItemType, FromExpression, Restrictions);
159+
cloneContext.QuerySourceMapping.AddMapping(this, new QuerySourceReferenceExpression(joinClause));
160+
return joinClause;
161+
}
162+
163+
public void MakeInner()
164+
{
165+
IsInner = true;
172166
}
173167

174168
public override string ToString()
175169
{
176-
return string.Format("from {0} {1} in {2}", ItemType.Name, ItemName, FromExpression);
170+
return string.Format("join {0} {1} in {2}", ItemType.Name, ItemName, FromExpression);
177171
}
178172
}
179-
}
173+
}

0 commit comments

Comments
 (0)