forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadonlyAccessor.cs
79 lines (70 loc) · 2.42 KB
/
ReadonlyAccessor.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;
using System.Reflection;
namespace NHibernate.Properties
{
/// <summary>
/// Access the mapped property through a Property <c>get</c> to get the value
/// and do nothing to set the value.
/// </summary>
/// <remarks>
/// This is useful to allow calculated properties in the domain that will never
/// be recovered from the DB but can be used for querying.
/// </remarks>
[Serializable]
public class ReadOnlyAccessor : IPropertyAccessor
{
/// <summary>
/// Initializes a new instance of <see cref="ReadOnlyAccessor"/>.
/// </summary>
public ReadOnlyAccessor()
{
}
#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="NoopAccessor.NoopSetter"/> to do nothing when trying to
/// se the value of the mapped Property
/// </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>
/// An instance of <see cref="NoopAccessor.NoopSetter"/>.
/// </returns>
public ISetter GetSetter(System.Type type, string propertyName)
{
return new NoopSetter();
}
public bool CanAccessThroughReflectionOptimizer
{
get { return true; }
}
#endregion
[Serializable]
private class NoopSetter : ISetter
{
public void Set(object target, object value) {}
public string PropertyName { get { return null; } }
public MethodInfo Method { get { return null; } }
}
}
}