forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignments.cs
58 lines (53 loc) · 2.22 KB
/
Assignments.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Linq.Visitors;
namespace NHibernate.Linq
{
/// <summary>
/// Class to hold assignments used in updates and inserts.
/// </summary>
/// <typeparam name="TSource">The type of the entity source of the insert or to update.</typeparam>
/// <typeparam name="TTarget">The type of the entity to insert or to update.</typeparam>
internal class Assignments<TSource, TTarget>
{
private readonly Dictionary<string, Expression> _assignments = new Dictionary<string, Expression>();
internal IReadOnlyDictionary<string, Expression> List => _assignments;
/// <summary>
/// Set the specified property.
/// </summary>
/// <typeparam name="TProp">The type of the property.</typeparam>
/// <param name="property">The property.</param>
/// <param name="expression">The expression that should be assigned to the property.</param>
/// <returns>The current assignments list.</returns>
public void Set<TProp>(Expression<Func<TTarget, TProp>> property, Expression<Func<TSource, TProp>> expression)
{
if (expression == null)
throw new ArgumentNullException(nameof(expression));
var member = GetMemberExpression(property);
_assignments.Add(member.GetMemberPath(), expression);
}
/// <summary>
/// Set the specified property.
/// </summary>
/// <typeparam name="TProp">The type of the property.</typeparam>
/// <param name="property">The property.</param>
/// <param name="value">The value.</param>
/// <returns>The current assignments list.</returns>
public void Set<TProp>(Expression<Func<TTarget, TProp>> property, TProp value)
{
var member = GetMemberExpression(property);
_assignments.Add(member.GetMemberPath(), Expression.Constant(value, typeof(TProp)));
}
private static MemberExpression GetMemberExpression<TProp>(Expression<Func<TTarget, TProp>> property)
{
if (property == null)
throw new ArgumentNullException(nameof(property));
var param = property.Parameters.Single();
var member = property.Body as MemberExpression ??
throw new ArgumentException($"The property expression must refer to a property of {param.Name}({param.Type.Name})", nameof(property));
return member;
}
}
}