1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Components
.Validator
18 using System
.Collections
;
19 using System
.Reflection
;
20 using System
.Resources
;
21 using System
.Threading
;
24 /// <see cref="IValidatorRegistry"/> implementation that
25 /// caches the reflection and custom attributes calls for better performance.
27 public class CachedValidationRegistry
: IValidatorRegistry
29 private static ResourceManager defaultResourceManager
;
30 private ResourceManager resourceManager
;
32 private readonly IDictionary propertiesPerType
= Hashtable
.Synchronized(new Hashtable());
33 private readonly IDictionary attrsPerProperty
= Hashtable
.Synchronized(new Hashtable());
36 /// Initializes the <see cref="CachedValidationRegistry"/> class.
38 static CachedValidationRegistry()
40 defaultResourceManager
=
41 new ResourceManager("Castle.Components.Validator.Messages",
42 typeof(CachedValidationRegistry
).Assembly
);
46 /// Initializes a new instance of the <see cref="CachedValidationRegistry"/> class.
48 public CachedValidationRegistry()
54 /// Initializes a new instance of the <see cref="CachedValidationRegistry"/> class.
56 /// <param name="resourceManager">The resource manager.</param>
57 public CachedValidationRegistry(ResourceManager resourceManager
)
59 this.resourceManager
= resourceManager
;
62 #region IValidatorRegistry Members
65 /// Gets all validators associated with a <see cref="Type"/>.
67 /// The validators returned are initialized.
70 /// <param name="validatorRunner">The validator runner.</param>
71 /// <param name="targetType">Target type.</param>
72 /// <param name="runWhen">Restrict the set returned to the phase specified</param>
73 /// <returns>A Validator array</returns>
74 public IValidator
[] GetValidators(ValidatorRunner validatorRunner
, Type targetType
, RunWhen runWhen
)
76 PropertyInfo
[] properties
= (PropertyInfo
[]) propertiesPerType
[targetType
];
78 if (properties
== null)
80 properties
= targetType
.GetProperties();
81 propertiesPerType
[targetType
] = properties
;
84 ArrayList list
= new ArrayList();
86 foreach (PropertyInfo prop
in properties
)
88 list
.AddRange(GetValidators(validatorRunner
, targetType
, prop
, runWhen
));
91 return (IValidator
[]) list
.ToArray(typeof (IValidator
));
95 /// Gets all validators associated with a property.
97 /// The validators returned are initialized.
100 /// <param name="validatorRunner">The validator runner.</param>
101 /// <param name="targetType">Target type.</param>
102 /// <param name="property">The property.</param>
103 /// <param name="runWhen">Restrict the set returned to the phase specified</param>
104 /// <returns>A Validator array</returns>
105 public IValidator
[] GetValidators(
106 ValidatorRunner validatorRunner
, Type targetType
, PropertyInfo property
,
109 object[] builders
= (object[]) attrsPerProperty
[property
];
111 if (builders
== null)
113 builders
= property
.GetCustomAttributes(typeof (IValidatorBuilder
), true);
114 attrsPerProperty
[property
] = builders
;
117 ArrayList validators
= new ArrayList();
119 foreach (IValidatorBuilder builder
in builders
)
121 IValidator validator
= builder
.Build(validatorRunner
, targetType
);
123 if (!IsValidatorOnPhase(validator
, runWhen
)) continue;
125 validator
.Initialize(this, property
);
126 validators
.Add(validator
);
129 return (IValidator
[]) validators
.ToArray(typeof (IValidator
));
133 /// Gets the string from resource by key
135 /// <param name="key">The key.</param>
136 /// <returns></returns>
137 public string GetStringFromResource(string key
)
139 if(resourceManager
!=null)
141 ResourceSet resourceSet
= resourceManager
.GetResourceSet(Thread
.CurrentThread
.CurrentCulture
, true, true);
142 string result
= resourceSet
.GetString(key
);
146 ResourceSet defaultResourceSetForCurrentThread
= defaultResourceManager
.GetResourceSet(Thread
.CurrentThread
.CurrentCulture
, true, true);
147 return defaultResourceSetForCurrentThread
.GetString(key
);
152 private static bool IsValidatorOnPhase(IValidator validator
, RunWhen when
)
154 if (validator
.RunWhen
== RunWhen
.Everytime
) return true;
156 return ((validator
.RunWhen
& when
) != 0);