Skip to content

Commit e5b64c3

Browse files
NH-3488 - Adjusting xml comments, minor refactoring (c#7, white-spaces, use of new ReflectionCache, ...). To be squashed.
1 parent 8876457 commit e5b64c3

11 files changed

+123
-131
lines changed

src/NHibernate/Hql/Ast/HqlTreeBuilder.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public HqlDelete Delete(HqlFrom @from)
3939
return new HqlDelete(_factory, @from);
4040
}
4141

42-
public HqlUpdate Update(HqlFrom @from,HqlSet set)
42+
public HqlUpdate Update(HqlFrom @from, HqlSet set)
4343
{
44-
return new HqlUpdate(_factory, @from,set);
44+
return new HqlUpdate(_factory, @from, set);
4545
}
4646

47-
public HqlUpdate Update(HqlVersioned versioned,HqlFrom @from, HqlSet set)
47+
public HqlUpdate Update(HqlVersioned versioned, HqlFrom @from, HqlSet set)
4848
{
4949
return new HqlUpdate(_factory, versioned, @from, set);
5050
}
@@ -53,7 +53,6 @@ public HqlInsert Insert(HqlInto into, HqlQuery query)
5353
{
5454
return new HqlInsert(_factory, into, query);
5555
}
56-
5756

5857
public HqlSelectFrom SelectFrom()
5958
{

src/NHibernate/Linq/Assignment.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ namespace NHibernate.Linq
77
/// </summary>
88
public class Assignment
99
{
10+
/// <summary>
11+
/// The assigned property.
12+
/// </summary>
1013
public string PropertyPath { get; set; }
14+
/// <summary>
15+
/// The value to assign.
16+
/// </summary>
1117
public Expression Expression { get; set; }
1218

13-
public Assignment(string propertyPath,Expression expression)
19+
public Assignment(string propertyPath, Expression expression)
1420
{
1521
PropertyPath = propertyPath;
1622
Expression = expression;

src/NHibernate/Linq/Assignments.cs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
using System.Linq.Expressions;
66
using System.Reflection;
77
using NHibernate.Linq.Visitors;
8+
using NHibernate.Util;
89

910
namespace NHibernate.Linq
1011
{
1112
public abstract class Assignments
1213
{
1314
protected static readonly ConstructorInfo DictionaryConstructorInfo = typeof(Dictionary<string, object>).GetConstructor(new[] { typeof(int) });
14-
protected static readonly MethodInfo DictionaryAddMethodInfo = typeof(Dictionary<string, object>).GetMethod("Add");
15+
protected static readonly MethodInfo DictionaryAddMethodInfo = ReflectHelper.GetMethod<Dictionary<string, object>>(d => d.Add(null, null));
1516
}
1617

1718
/// <summary>
18-
/// Class to hold assignments used in updates and inserts
19+
/// Class to hold assignments used in updates and inserts.
1920
/// </summary>
20-
/// <typeparam name="TInput">The type of the input.</typeparam>
21-
/// <typeparam name="TOutput">The type of the output.</typeparam>
22-
public class Assignments<TInput, TOutput> : Assignments
21+
/// <typeparam name="TSource">The type of the entity source of the insert or to update.</typeparam>
22+
/// <typeparam name="TTarget">The type of the entity to insert or to update.</typeparam>
23+
public class Assignments<TSource, TTarget> : Assignments
2324
{
2425
private readonly List<Assignment> _sets = new List<Assignment>();
2526

@@ -29,16 +30,12 @@ public class Assignments<TInput, TOutput> : Assignments
2930
/// <typeparam name="TProp">The type of the property.</typeparam>
3031
/// <param name="property">The property.</param>
3132
/// <param name="expression">The expression that should be assigned to the property.</param>
32-
/// <returns></returns>
33-
public Assignments<TInput, TOutput> Set<TProp>(Expression<Func<TOutput, TProp>> property, Expression<Func<TInput, TProp>> expression)
33+
/// <returns>The current assignments list.</returns>
34+
public Assignments<TSource, TTarget> Set<TProp>(Expression<Func<TTarget, TProp>> property, Expression<Func<TSource, TProp>> expression)
3435
{
35-
if (property == null)
36-
throw new ArgumentNullException(nameof(property));
3736
if (expression == null)
3837
throw new ArgumentNullException(nameof(expression));
39-
var param = property.Parameters.Single();
40-
var member = property.Body as MemberExpression ??
41-
throw new ArgumentException("The property expression must refer to a property of " + param.Name + "("+ param.Type.Name + ")", nameof(property));
38+
var member = GetMemberExpression(property);
4239
_sets.Add(new Assignment(member.GetMemberPath(), expression));
4340
return this;
4441
}
@@ -49,25 +46,31 @@ public Assignments<TInput, TOutput> Set<TProp>(Expression<Func<TOutput, TProp>>
4946
/// <typeparam name="TProp">The type of the property.</typeparam>
5047
/// <param name="property">The property.</param>
5148
/// <param name="value">The value.</param>
52-
/// <returns></returns>
53-
public Assignments<TInput, TOutput> Set<TProp>(Expression<Func<TOutput, TProp>> property, TProp value)
49+
/// <returns>The current assignments list.</returns>
50+
public Assignments<TSource, TTarget> Set<TProp>(Expression<Func<TTarget, TProp>> property, TProp value)
51+
{
52+
var member = GetMemberExpression(property);
53+
_sets.Add(new Assignment(member.GetMemberPath(), Expression.Constant(value, typeof(TProp))));
54+
return this;
55+
}
56+
57+
private static MemberExpression GetMemberExpression<TProp>(Expression<Func<TTarget, TProp>> property)
5458
{
5559
if (property == null)
5660
throw new ArgumentNullException(nameof(property));
5761
var param = property.Parameters.Single();
5862
var member = property.Body as MemberExpression ??
59-
throw new ArgumentException("The property expression must refer to a property of " + param.Name + "(" + param.Type.Name + ")", nameof(property));
60-
_sets.Add(new Assignment(member.GetMemberPath(), Expression.Constant(value, typeof(TProp))));
61-
return this;
63+
throw new ArgumentException($"The property expression must refer to a property of {param.Name}({param.Type.Name})", nameof(property));
64+
return member;
6265
}
6366

6467
/// <summary>
65-
/// Converts the assignments into a to lambda expression, which creates a Dictionary&lt;string,object%gt;.
68+
/// Converts the assignments into a lambda expression, which creates a Dictionary&lt;string,object%gt;.
6669
/// </summary>
67-
/// <returns></returns>
70+
/// <returns>A lambda expression representing the assignments.</returns>
6871
public LambdaExpression ConvertToDictionaryExpression()
6972
{
70-
var param = Expression.Parameter(typeof(TInput));
73+
var param = Expression.Parameter(typeof(TSource));
7174
var inits = new List<ElementInit>();
7275
foreach (var set in _sets)
7376
{
@@ -77,44 +80,43 @@ public LambdaExpression ConvertToDictionaryExpression()
7780
setter = setterLambda.Body.Replace(setterLambda.Parameters.First(), param);
7881
}
7982
inits.Add(Expression.ElementInit(DictionaryAddMethodInfo, Expression.Constant(set.PropertyPath),
80-
Expression.Convert(
81-
setter,
82-
typeof(object))));
83-
83+
Expression.Convert(setter, typeof(object))));
8484
}
8585

86-
8786
//The ListInit is intentionally "infected" with the lambda parameter (param), in the form of an IIF.
8887
//The only relevance is to make sure that the ListInit is not evaluated by the PartialEvaluatingExpressionTreeVisitor,
89-
//which could turn it into a Constant
88+
//which could turn it into a Constant
9089
var listInit = Expression.ListInit(
9190
Expression.New(
9291
DictionaryConstructorInfo,
9392
Expression.Condition(
94-
Expression.Equal(param, Expression.Constant(null, typeof(TInput))),
93+
Expression.Equal(param, Expression.Constant(null, typeof(TSource))),
9594
Expression.Constant(_sets.Count),
9695
Expression.Constant(_sets.Count))),
9796
inits);
9897

99-
100-
10198
return Expression.Lambda(listInit, param);
10299
}
103100

104-
public static Assignments<TInput, TOutput> FromExpression(Expression<Func<TInput, TOutput>> expression)
101+
/// <summary>
102+
/// Converts a members initialization expression to assignments. Unset members are ignored and left untouched.
103+
/// </summary>
104+
/// <param name="expression">The expression to convert.</param>
105+
/// <returns>The corresponding assignments.</returns>
106+
public static Assignments<TSource, TTarget> FromExpression(Expression<Func<TSource, TTarget>> expression)
105107
{
106108
if (expression == null)
107109
throw new ArgumentNullException(nameof(expression));
108-
var instance = new Assignments<TInput, TOutput>();
110+
var instance = new Assignments<TSource, TTarget>();
109111
var memberInitExpression = expression.Body as MemberInitExpression ??
110-
throw new ArgumentException("The expression must be member initialization, e.g. x => new Dog{Name = x.Name,Age = x.Age + 5}");
111-
112+
throw new ArgumentException("The expression must be member initialization, e.g. x => new Dog { Name = x.Name, Age = x.Age + 5 }");
113+
112114
AddSetsFromBindings(memberInitExpression.Bindings, instance, "", expression.Parameters);
113-
115+
114116
return instance;
115117
}
116118

117-
private static void AddSetsFromBindings(IEnumerable<MemberBinding> bindings, Assignments<TInput, TOutput> instance, string path, ReadOnlyCollection<ParameterExpression> parameters)
119+
private static void AddSetsFromBindings(IEnumerable<MemberBinding> bindings, Assignments<TSource, TTarget> instance, string path, ReadOnlyCollection<ParameterExpression> parameters)
118120
{
119121
foreach (var binding in bindings)
120122
{
@@ -124,12 +126,12 @@ private static void AddSetsFromBindings(IEnumerable<MemberBinding> bindings, Ass
124126
}
125127
else if (binding.BindingType == MemberBindingType.MemberBinding) // {Property={SubProperty="Value}}
126128
{
127-
AddSetsFromBindings(((MemberMemberBinding) binding).Bindings, instance, path + "." + binding.Member.Name, parameters);
129+
AddSetsFromBindings(((MemberMemberBinding)binding).Bindings, instance, path + "." + binding.Member.Name, parameters);
128130
}
129131
}
130132
}
131133

132-
private static void AddSetsFromAssignment(MemberAssignment assignment, Assignments<TInput, TOutput> instance, string path, ReadOnlyCollection<ParameterExpression> parameters)
134+
private static void AddSetsFromAssignment(MemberAssignment assignment, Assignments<TSource, TTarget> instance, string path, ReadOnlyCollection<ParameterExpression> parameters)
133135
{
134136
// {Property=new Instance{SubProperty="Value"}}
135137
if (assignment.Expression is MemberInitExpression memberInit)

src/NHibernate/Linq/InsertSyntax.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,30 @@
55

66
namespace NHibernate.Linq
77
{
8-
public class InsertSyntax<TInput>
8+
/// <summary>
9+
/// An insert object on which entities to insert can be specified.
10+
/// </summary>
11+
/// <typeparam name="TSource">The type of the entities selected as source of the insert.</typeparam>
12+
public class InsertSyntax<TSource>
913
{
10-
private readonly IQueryable<TInput> _query;
14+
private readonly IQueryable<TSource> _query;
1115

12-
internal InsertSyntax(IQueryable<TInput> query)
16+
internal InsertSyntax(IQueryable<TSource> query)
1317
{
1418
_query = query;
1519
}
1620

1721
/// <summary>
1822
/// Executes the insert, using the specified assignments.
1923
/// </summary>
20-
/// <typeparam name="TOutput">The type of the output.</typeparam>
24+
/// <typeparam name="TTarget">The type of the entities to insert.</typeparam>
2125
/// <param name="assignmentActions">The assignments.</param>
22-
/// <returns></returns>
23-
public int Into<TOutput>(Action<Assignments<TInput, TOutput>> assignmentActions)
26+
/// <returns>The number of inserted entities.</returns>
27+
public int Into<TTarget>(Action<Assignments<TSource, TTarget>> assignmentActions)
2428
{
2529
if (assignmentActions == null)
2630
throw new ArgumentNullException(nameof(assignmentActions));
27-
var assignments = new Assignments<TInput, TOutput>();
31+
var assignments = new Assignments<TSource, TTarget>();
2832
assignmentActions.Invoke(assignments);
2933
return InsertInto(_query, assignments);
3034
}
@@ -34,21 +38,20 @@ public int Into<TOutput>(Action<Assignments<TInput, TOutput>> assignmentActions)
3438
/// </summary>
3539
/// <typeparam name="TOutput">The type of the output.</typeparam>
3640
/// <param name="expression">The expression.</param>
37-
/// <returns></returns>
38-
public int As<TOutput>(Expression<Func<TInput, TOutput>> expression)
41+
/// <returns>The number of inserted entities.</returns>
42+
public int As<TOutput>(Expression<Func<TSource, TOutput>> expression)
3943
{
40-
var assignments = Assignments<TInput, TOutput>.FromExpression(expression);
44+
var assignments = Assignments<TSource, TOutput>.FromExpression(expression);
4145
return InsertInto(_query, assignments);
4246
}
4347

44-
internal int InsertInto<TOutput>(IQueryable<TInput> query, Assignments<TInput, TOutput> assignments)
48+
internal int InsertInto<TOutput>(IQueryable<TSource> query, Assignments<TSource, TOutput> assignments)
4549
{
46-
var nhQueryable = query as QueryableBase<TInput> ??
50+
var nhQueryable = query as QueryableBase<TSource> ??
4751
throw new NotSupportedException("Query needs to be of type QueryableBase<T>");
4852

4953
var provider = (DefaultQueryProvider)query.Provider;
5054
return provider.ExecuteInsert(nhQueryable, assignments);
5155
}
52-
5356
}
5457
}

src/NHibernate/Linq/LinqExtensionMethods.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public static IEnumerable<T> ToFuture<T>(this IQueryable<T> query)
8383
if (nhQueryable == null)
8484
throw new NotSupportedException("Query needs to be of type QueryableBase<T>");
8585

86-
var provider = (INhQueryProvider) nhQueryable.Provider;
86+
var provider = (INhQueryProvider)nhQueryable.Provider;
8787
var future = provider.ExecuteFuture(nhQueryable.Expression);
88-
return (IEnumerable<T>) future;
88+
return (IEnumerable<T>)future;
8989
}
9090

9191
public static IFutureValue<T> ToFutureValue<T>(this IQueryable<T> query)
@@ -94,14 +94,14 @@ public static IFutureValue<T> ToFutureValue<T>(this IQueryable<T> query)
9494
if (nhQueryable == null)
9595
throw new NotSupportedException("Query needs to be of type QueryableBase<T>");
9696

97-
var provider = (INhQueryProvider) nhQueryable.Provider;
97+
var provider = (INhQueryProvider)nhQueryable.Provider;
9898
var future = provider.ExecuteFuture(nhQueryable.Expression);
9999
if (future is IEnumerable<T>)
100100
{
101-
return new FutureValue<T>(() => ((IEnumerable<T>) future));
101+
return new FutureValue<T>(() => ((IEnumerable<T>)future));
102102
}
103103

104-
return (IFutureValue<T>) future;
104+
return (IFutureValue<T>)future;
105105
}
106106

107107
public static T MappedAs<T>(this T parameter, IType type)
@@ -115,53 +115,50 @@ public static IFutureValue<TResult> ToFutureValue<T, TResult>(this IQueryable<T>
115115
if (nhQueryable == null)
116116
throw new NotSupportedException("Query needs to be of type QueryableBase<T>");
117117

118-
var provider = (INhQueryProvider) query.Provider;
118+
var provider = (INhQueryProvider)query.Provider;
119119

120120
var expression = ReplacingExpressionTreeVisitor.Replace(selector.Parameters.Single(),
121121
query.Expression,
122122
selector.Body);
123123

124-
return (IFutureValue<TResult>) provider.ExecuteFuture(expression);
124+
return (IFutureValue<TResult>)provider.ExecuteFuture(expression);
125125
}
126126

127-
128127
/// <summary>
129-
/// Deletes all entities in the specified query. The delete operation is performed in the database.
128+
/// Deletes all entities selected by the specified query. The delete operation is performed in the database without reading the entities out of it.
130129
/// </summary>
131-
/// <typeparam name="T"></typeparam>
130+
/// <typeparam name="T">The type of the entities to delete.</typeparam>
132131
/// <param name="query">The query.</param>
133-
/// <returns></returns>
132+
/// <returns>The number of deleted entities.</returns>
134133
public static int Delete<T>(this IQueryable<T> query)
135134
{
136-
var nhQueryable = query as QueryableBase<T>;
137-
if (nhQueryable == null)
135+
var nhQueryable = query as QueryableBase<T> ??
138136
throw new NotSupportedException("Query needs to be of type QueryableBase<T>");
139137

140-
var provider =nhQueryable.Provider as DefaultQueryProvider;
138+
var provider = nhQueryable.Provider as DefaultQueryProvider;
141139
return provider.ExecuteDelete(query.Expression);
142140
}
143141

144142
/// <summary>
145-
/// Updates the entities in the query, using the specified assignments. The update operation is performed in the database.
143+
/// Initiate an update for the entities selected by the query. The update operation will be performed in the database without reading the entities out of it.
146144
/// </summary>
147-
/// <typeparam name="T"></typeparam>
145+
/// <typeparam name="T">The type of the entities to update.</typeparam>
148146
/// <param name="query">The query.</param>
149-
/// <returns></returns>
147+
/// <returns>An update object on which values to update can be specified.</returns>
150148
public static UpdateSyntax<T> Update<T>(this IQueryable<T> query)
151149
{
152150
return new UpdateSyntax<T>(query);
153151
}
154152

155153
/// <summary>
156-
/// Inserts new entities into the database, using other stored entities as a source. Uses INSERT INTO [...] SELECT FROM [...] in the database.
154+
/// Initiate an insert using selected entities as a source. Will use <c>INSERT INTO [...] SELECT FROM [...]</c> in the database.
157155
/// </summary>
158-
/// <typeparam name="TInput">The type of the input.</typeparam>
156+
/// <typeparam name="TSource">The type of the selected entities.</typeparam>
159157
/// <param name="query">The query.</param>
160-
/// <returns></returns>
161-
public static InsertSyntax<TInput> Insert<TInput>(this IQueryable<TInput> query)
158+
/// <returns>An insert object on which entities to insert can be specified.</returns>
159+
public static InsertSyntax<TSource> Insert<TSource>(this IQueryable<TSource> query)
162160
{
163-
return new InsertSyntax<TInput>(query);
161+
return new InsertSyntax<TSource>(query);
164162
}
165-
166163
}
167164
}

src/NHibernate/Linq/NhLinqDeleteExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public NhLinqDeleteExpression(Expression expression, ISessionFactoryImplementor
1616
protected override ExpressionToHqlTranslationResults GenerateHqlQuery(QueryModel queryModel, VisitorParameters visitorParameters)
1717
{
1818
visitorParameters.EntityType = Type;
19-
return QueryModelVisitor.GenerateHqlQuery(queryModel, visitorParameters, true,QueryMode.Delete);
19+
return QueryModelVisitor.GenerateHqlQuery(queryModel, visitorParameters, true, QueryMode.Delete);
2020
}
2121
}
2222
}

0 commit comments

Comments
 (0)