forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoSetterAccessor.cs
79 lines (72 loc) · 2.97 KB
/
NoSetterAccessor.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
namespace NHibernate.Properties
{
/// <summary>
/// Access the mapped property through a Property <c>get</c> to get the value
/// and go directly to the Field to set the value.
/// </summary>
/// <remarks>
/// This is most useful because Classes can provider a get for the Property
/// that is the <c><id></c> but tell NHibernate there is no setter for the Property
/// so the value should be written directly to the field.
/// </remarks>
[Serializable]
public class NoSetterAccessor : IPropertyAccessor
{
private readonly IFieldNamingStrategy namingStrategy;
/// <summary>
/// Initializes a new instance of <see cref="NoSetterAccessor"/>.
/// </summary>
/// <param name="namingStrategy">The <see cref="IFieldNamingStrategy"/> to use.</param>
public NoSetterAccessor(IFieldNamingStrategy namingStrategy)
{
this.namingStrategy = namingStrategy;
}
#region IPropertyAccessor Members
/// <summary>
/// Creates an <see cref="BasicPropertyAccessor.BasicGetter"/> to <c>get</c> the value from the Property.
/// </summary>
/// <param name="type">The <see cref="System.Type"/> to find the Property in.</param>
/// <param name="propertyName">The name of the mapped Property to get.</param>
/// <returns>
/// The <see cref="BasicPropertyAccessor.BasicGetter"/> to use to get the value of the Property from an
/// instance of the <see cref="System.Type"/>.</returns>
/// <exception cref="PropertyNotFoundException" >
/// Thrown when a Property specified by the <c>propertyName</c> could not
/// be found in the <see cref="System.Type"/>.
/// </exception>
public IGetter GetGetter(System.Type type, string propertyName)
{
BasicPropertyAccessor.BasicGetter result = BasicPropertyAccessor.GetGetterOrNull(type, propertyName);
if (result == null)
{
throw new PropertyNotFoundException(type, propertyName, "getter");
}
return result;
}
/// <summary>
/// Create a <see cref="FieldAccessor.FieldSetter"/> to <c>set</c> the value of the mapped Property
/// through a <c>Field</c>.
/// </summary>
/// <param name="type">The <see cref="System.Type"/> to find the mapped Property in.</param>
/// <param name="propertyName">The name of the mapped Property to set.</param>
/// <returns>
/// The <see cref="FieldAccessor.FieldSetter"/> to use to set the value of the Property on an
/// instance of the <see cref="System.Type"/>.
/// </returns>
/// <exception cref="PropertyNotFoundException" >
/// Thrown when a Field for the Property specified by the <c>propertyName</c> using the
/// <see cref="IFieldNamingStrategy"/> could not be found in the <see cref="System.Type"/>.
/// </exception>
public ISetter GetSetter(System.Type type, string propertyName)
{
string fieldName = namingStrategy.GetFieldName(propertyName);
return new FieldAccessor.FieldSetter(FieldAccessor.GetField(type, fieldName), type, fieldName);
}
public bool CanAccessThroughReflectionOptimizer
{
get { return true; }
}
#endregion
}
}