0
|
1 using System;
|
|
2 using System.ComponentModel;
|
|
3 using System.Reflection;
|
|
4 using BLToolkit.Mapping;
|
|
5 using BLToolkit.Reflection;
|
|
6
|
|
7 namespace BLToolkit.Validation
|
|
8 {
|
|
9 public class ValidationContext
|
|
10 {
|
|
11 public delegate bool IsNullHandler(ValidationContext context);
|
|
12
|
|
13 private object _object;
|
|
14 public object Object
|
|
15 {
|
|
16 get { return _object; }
|
|
17 set { _object = value; }
|
|
18 }
|
|
19
|
|
20 private TypeAccessor _typeAccessor;
|
|
21 public TypeAccessor TypeAccessor
|
|
22 {
|
|
23 get
|
|
24 {
|
|
25 if (_typeAccessor == null)
|
|
26 _typeAccessor = TypeAccessor.GetAccessor(_object.GetType());
|
|
27
|
|
28 return _typeAccessor;
|
|
29 }
|
|
30 }
|
|
31
|
|
32 private PropertyDescriptor _propertyDescriptor;
|
|
33 public PropertyDescriptor PropertyDescriptor
|
|
34 {
|
|
35 get { return _propertyDescriptor; }
|
|
36 set { _propertyDescriptor = value; }
|
|
37 }
|
|
38
|
|
39 private IsNullHandler _isNull;
|
|
40 public IsNullHandler IsNull
|
|
41 {
|
|
42 get { return _isNull; }
|
|
43 set { _isNull = value; }
|
|
44 }
|
|
45
|
|
46 public bool IsValueNull
|
|
47 {
|
|
48 get { return _isNull(this); }
|
|
49 }
|
|
50
|
|
51 private object _value;
|
|
52 public object Value
|
|
53 {
|
|
54 get { return _value; }
|
|
55 set
|
|
56 {
|
|
57 _value = value;
|
|
58 _nullValue = null;
|
|
59 }
|
|
60 }
|
|
61
|
|
62 private MemberAccessor _memberAccessor;
|
|
63 public MemberAccessor MemberAccessor
|
|
64 {
|
|
65 get { return _memberAccessor; }
|
|
66 set
|
|
67 {
|
|
68 _memberAccessor = value;
|
|
69 _memberInfo = value != null? value.MemberInfo: null;
|
|
70 }
|
|
71 }
|
|
72
|
|
73 private MemberInfo _memberInfo;
|
|
74 public MemberInfo MemberInfo
|
|
75 {
|
|
76 get { return _memberInfo; }
|
|
77 }
|
|
78
|
|
79 private object _nullValue;
|
|
80 public object NullValue
|
|
81 {
|
|
82 get
|
|
83 {
|
|
84 if (_nullValue == null)
|
|
85 {
|
|
86 if (_value == null)
|
|
87 throw new InvalidOperationException("NullValue is undefined when Value == null");
|
|
88
|
|
89 ObjectMapper om = Map.GetObjectMapper(Object.GetType());
|
|
90 MemberMapper mm = om[MemberName, true];
|
|
91
|
|
92 _nullValue =
|
|
93 mm != null && mm.MapMemberInfo.Nullable && mm.MapMemberInfo.NullValue != null?
|
|
94 mm.MapMemberInfo.NullValue:
|
|
95 TypeAccessor.GetNullValue(Value.GetType());
|
|
96
|
|
97 if (_nullValue == null)
|
|
98 _nullValue = DBNull.Value;
|
|
99 }
|
|
100
|
|
101 return _nullValue;
|
|
102
|
|
103 }
|
|
104 }
|
|
105
|
|
106 public string MemberName
|
|
107 {
|
|
108 get
|
|
109 {
|
|
110 return
|
|
111 _memberInfo != null? _memberInfo.Name:
|
|
112 #if !SILVERLIGHT
|
|
113 _propertyDescriptor != null? _propertyDescriptor.Name:
|
|
114 #endif
|
|
115 null;
|
|
116 }
|
|
117 }
|
|
118 }
|
|
119 }
|