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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
#if SILVERLIGHT
using System.Reflection;
#endif
namespace System.ComponentModel.DataAnnotations {
/// <summary>
/// Describes the context in which a validation is being performed.
/// </summary>
/// <remarks>
/// This class contains information describing the instance on which
/// validation is being performed.
/// <para>
/// It supports <see cref="IServiceProvider"/> so that custom validation
/// code can acquire additional services to help it perform its validation.
/// </para>
/// <para>
/// An <see cref="Items"/> property bag is available for additional contextual
/// information about the validation. Values stored in <see cref="Items"/>
/// will be available to validation methods that use this <see cref="ValidationContext"/>
/// </para>
/// </remarks>
[SuppressMessage("Microsoft.Usage", "CA2302:FlagServiceProviders", Justification = "The actual IServiceProvider implementation lies with the underlying service provider.")]
public sealed class ValidationContext : IServiceProvider
{
#region Member Fields
private Func<Type, object> _serviceProvider;
private object _objectInstance;
private string _memberName;
private string _displayName;
private Dictionary<object, object> _items;
#endregion
#region Constructors
/// <summary>
/// Construct a <see cref="ValidationContext"/> for a given object instance being validated.
/// </summary>
/// <param name="instance">The object instance being validated. It cannot be <c>null</c>.</param>
/// <exception cref="ArgumentNullException">When <paramref name="instance"/> is <c>null</c></exception>
public ValidationContext(object instance)
: this(instance, null, null) {
}
/// <summary>
/// Construct a <see cref="ValidationContext"/> for a given object instance and an optional
/// property bag of <paramref name="items"/>.
/// </summary>
/// <param name="instance">The object instance being validated. It cannot be null.</param>
/// <param name="items">Optional set of key/value pairs to make available to consumers via <see cref="Items"/>.
/// If null, an empty dictionary will be created. If not null, the set of key/value pairs will be copied into a
/// new dictionary, preventing consumers from modifying the original dictionary.
/// </param>
/// <exception cref="ArgumentNullException">When <paramref name="instance"/> is <c>null</c></exception>
public ValidationContext(object instance, IDictionary<object, object> items)
: this(instance, null, items) {
}
#if SILVERLIGHT
/// <summary>
/// Construct a <see cref="ValidationContext"/> for a given object instance, an optional <paramref name="serviceProvider"/>, and an optional
/// property bag of <paramref name="items"/>.
/// </summary>
/// <param name="instance">The object instance being validated. It cannot be null.</param>
/// <param name="serviceProvider">
/// Optional <see cref="IServiceProvider"/> to use when <see cref="GetService"/> is called.
/// If it is null, <see cref="GetService"/> will always return null.
/// </param>
/// <param name="items">Optional set of key/value pairs to make available to consumers via <see cref="Items"/>.
/// If null, an empty dictionary will be created. If not null, the set of key/value pairs will be copied into a
/// new dictionary, preventing consumers from modifying the original dictionary.
/// </param>
/// <exception cref="ArgumentNullException">When <paramref name="instance"/> is <c>null</c></exception>
#else
/// <summary>
/// Construct a <see cref="ValidationContext"/> for a given object instance, an optional <paramref name="serviceProvider"/>, and an optional
/// property bag of <paramref name="items"/>.
/// </summary>
/// <param name="instance">The object instance being validated. It cannot be null.</param>
/// <param name="serviceProvider">
/// Optional <see cref="IServiceProvider"/> to use when <see cref="GetService"/> is called.
/// <para>
/// If the <paramref name="serviceProvider"/> specified implements <see cref="Design.IServiceContainer"/>,
/// then it will be used as the <see cref="ServiceContainer"/> but its services can still be retrieved
/// through <see cref="GetService"/> as well.
/// </para>
/// </param>
/// <param name="items">Optional set of key/value pairs to make available to consumers via <see cref="Items"/>.
/// If null, an empty dictionary will be created. If not null, the set of key/value pairs will be copied into a
/// new dictionary, preventing consumers from modifying the original dictionary.
/// </param>
/// <exception cref="ArgumentNullException">When <paramref name="instance"/> is <c>null</c></exception>
#endif
public ValidationContext(object instance, IServiceProvider serviceProvider, IDictionary<object, object> items) {
if (instance == null) {
throw new ArgumentNullException("instance");
}
if (serviceProvider != null) {
this.InitializeServiceProvider(serviceType => serviceProvider.GetService(serviceType));
}
#if !SILVERLIGHT
Design.IServiceContainer container = serviceProvider as Design.IServiceContainer;
if (container != null) {
this._serviceContainer = new ValidationContextServiceContainer(container);
} else {
this._serviceContainer = new ValidationContextServiceContainer();
}
#endif
if (items != null) {
this._items = new Dictionary<object, object>(items);
} else {
this._items = new Dictionary<object, object>();
}
this._objectInstance = instance;
}
#endregion
#region Properties
/// <summary>
/// Gets the object instance being validated. While it will not be null, the state of the instance is indeterminate
/// as it might only be partially initialized during validation.
/// <para>Consume this instance with caution!</para>
/// </summary>
/// <remarks>
/// During validation, especially property-level validation, the object instance might be in an indeterminate state.
/// For example, the property being validated, as well as other properties on the instance might not have been
/// updated to their new values.
/// </remarks>
public object ObjectInstance {
get {
return this._objectInstance;
}
}
/// <summary>
/// Gets the type of the object being validated. It will not be null.
/// </summary>
public Type ObjectType {
get {
#if SILVERLIGHT
return this.ObjectInstance.GetCustomOrCLRType();
#else
return this.ObjectInstance.GetType();
#endif
}
}
/// <summary>
/// Gets or sets the user-visible name of the type or property being validated.
/// </summary>
/// <value>If this name was not explicitly set, this property will consult an associated <see cref="DisplayAttribute"/>
/// to see if can use that instead. Lacking that, it returns <see cref="MemberName"/>. The <see cref="ObjectInstance"/>
/// type name will be used if MemberName has not been set.
/// </value>
public string DisplayName {
get {
if (string.IsNullOrEmpty(this._displayName)) {
this._displayName = this.GetDisplayName();
if (string.IsNullOrEmpty(this._displayName)) {
this._displayName = this.MemberName;
if (string.IsNullOrEmpty(this._displayName)) {
this._displayName = this.ObjectType.Name;
}
}
}
return this._displayName;
}
set {
if (string.IsNullOrEmpty(value)) {
throw new ArgumentNullException("value");
}
this._displayName = value;
}
}
/// <summary>
/// Gets or sets the name of the type or property being validated.
/// </summary>
/// <value>This name reflects the API name of the member being validated, not a localized name. It should be set
/// only for property or parameter contexts.</value>
public string MemberName {
get {
return this._memberName;
}
set {
this._memberName = value;
}
}
/// <summary>
/// Gets the dictionary of key/value pairs associated with this context.
/// </summary>
/// <value>This property will never be null, but the dictionary may be empty. Changes made
/// to items in this dictionary will never affect the original dictionary specified in the constructor.</value>
public IDictionary<object, object> Items {
get {
return this._items;
}
}
#endregion
#region Methods
/// <summary>
/// Looks up the display name using the DisplayAttribute attached to the respective type or property.
/// </summary>
/// <returns>A display-friendly name of the member represented by the <see cref="MemberName"/>.</returns>
private string GetDisplayName() {
string displayName = null;
ValidationAttributeStore store = ValidationAttributeStore.Instance;
DisplayAttribute displayAttribute = null;
if (string.IsNullOrEmpty(this._memberName)) {
displayAttribute = store.GetTypeDisplayAttribute(this);
} else if (store.IsPropertyContext(this)) {
displayAttribute = store.GetPropertyDisplayAttribute(this);
}
if (displayAttribute != null) {
displayName = displayAttribute.GetName();
}
return displayName ?? this.MemberName;
}
/// <summary>
/// Initializes the <see cref="ValidationContext"/> with a service provider that can return
/// service instances by <see cref="Type"/> when <see cref="GetService"/> is called.
/// </summary>
/// <param name="serviceProvider">
/// A <see cref="Func{T, TResult}"/> that can return service instances given the
/// desired <see cref="Type"/> when <see cref="GetService"/> is called.
/// If it is <c>null</c>, <see cref="GetService"/> will always return <c>null</c>.
/// </param>
public void InitializeServiceProvider(Func<Type, object> serviceProvider) {
this._serviceProvider = serviceProvider;
}
#endregion
#region IServiceProvider Members
#if SILVERLIGHT
/// <summary>
/// See <see cref="IServiceProvider.GetService(Type)"/>.
/// </summary>
/// <param name="serviceType">The type of the service needed.</param>
/// <returns>An instance of that service or null if it is not available.</returns>
#else
/// <summary>
/// See <see cref="IServiceProvider.GetService(Type)"/>.
/// When the <see cref="ServiceContainer"/> is in use, it will be used
/// first to retrieve the requested service. If the <see cref="ServiceContainer"/>
/// is not being used or it cannot resolve the service, then the
/// <see cref="IServiceProvider"/> provided to this <see cref="ValidationContext"/>
/// will be queried for the service type.
/// </summary>
/// <param name="serviceType">The type of the service needed.</param>
/// <returns>An instance of that service or null if it is not available.</returns>
#endif
public object GetService(Type serviceType) {
object service = null;
#if !SILVERLIGHT
if (this._serviceContainer != null) {
service = this._serviceContainer.GetService(serviceType);
}
#endif
if (service == null && this._serviceProvider != null) {
service = this._serviceProvider(serviceType);
}
return service;
}
#endregion
#if !SILVERLIGHT
#region Service Container
private Design.IServiceContainer _serviceContainer;
/// <summary>
/// A <see cref="Design.IServiceContainer"/> that can be used for adding,
/// removing, and getting services during validation. <see cref="GetService"/>
/// will query into this container as well as the <see cref="IServiceProvider"/>
/// specified in the constructor.
/// </summary>
/// <remarks>
/// If the <see cref="IServiceProvider"/> specified to the constructor implements
/// <see cref="Design.IServiceContainer"/>, then it will be used as the
/// <see cref="ServiceContainer"/>, otherwise an empty container will be initialized.
/// </remarks>
public Design.IServiceContainer ServiceContainer {
get {
if (this._serviceContainer == null) {
this._serviceContainer = new ValidationContextServiceContainer();
}
return this._serviceContainer;
}
}
/// <summary>
/// Private implementation of <see cref="Design.IServiceContainer"/> to act
/// as a default service container on <see cref="ValidationContext"/>.
/// </summary>
[SuppressMessage("Microsoft.Usage", "CA2302:FlagServiceProviders", Justification = "ValidationContext does not need to work with COM interop types. So this is fine.")]
private class ValidationContextServiceContainer : Design.IServiceContainer
{
#region Member Fields
private Design.IServiceContainer _parentContainer;
private Dictionary<Type, object> _services = new Dictionary<Type, object>();
private readonly object _lock = new object();
#endregion
#region Constructors
/// <summary>
/// Constructs a new service container that does not have a parent container
/// </summary>
internal ValidationContextServiceContainer() {
}
/// <summary>
/// Contstructs a new service container that has a parent container, making this container
/// a wrapper around the parent container. Calls to <c>AddService</c> and <c>RemoveService</c>
/// will promote to the parent container by default, unless <paramref name="promote"/> is
/// specified as <c>false</c> on those calls.
/// </summary>
/// <param name="parentContainer">The parent container to wrap into this container.</param>
internal ValidationContextServiceContainer(Design.IServiceContainer parentContainer) {
this._parentContainer = parentContainer;
}
#endregion
#region IServiceContainer Members
[SuppressMessage("Microsoft.Usage", "CA2301:EmbeddableTypesInContainersRule", MessageId = "_services", Justification = "ValidationContext does not need to work with COM interop types. So this is fine.")]
public void AddService(Type serviceType, Design.ServiceCreatorCallback callback, bool promote)
{
if (promote && this._parentContainer != null) {
this._parentContainer.AddService(serviceType, callback, promote);
} else {
lock (this._lock) {
if (this._services.ContainsKey(serviceType)) {
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.ValidationContextServiceContainer_ItemAlreadyExists, serviceType), "serviceType");
}
this._services.Add(serviceType, callback);
}
}
}
public void AddService(Type serviceType, Design.ServiceCreatorCallback callback) {
this.AddService(serviceType, callback, true);
}
[SuppressMessage("Microsoft.Usage", "CA2301:EmbeddableTypesInContainersRule", MessageId = "_services", Justification = "ValidationContext does not need to work with COM interop types. So this is fine.")]
public void AddService(Type serviceType, object serviceInstance, bool promote)
{
if (promote && this._parentContainer != null) {
this._parentContainer.AddService(serviceType, serviceInstance, promote);
} else {
lock (this._lock) {
if (this._services.ContainsKey(serviceType)) {
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.ValidationContextServiceContainer_ItemAlreadyExists, serviceType), "serviceType");
}
this._services.Add(serviceType, serviceInstance);
}
}
}
public void AddService(Type serviceType, object serviceInstance) {
this.AddService(serviceType, serviceInstance, true);
}
public void RemoveService(Type serviceType, bool promote) {
lock (this._lock) {
if (this._services.ContainsKey(serviceType)) {
this._services.Remove(serviceType);
}
}
if (promote && this._parentContainer != null) {
this._parentContainer.RemoveService(serviceType);
}
}
public void RemoveService(Type serviceType) {
this.RemoveService(serviceType, true);
}
#endregion
#region IServiceProvider Members
public object GetService(Type serviceType) {
if (serviceType == null) {
throw new ArgumentNullException("serviceType");
}
object service = null;
this._services.TryGetValue(serviceType, out service);
if (service == null && this._parentContainer != null) {
service = this._parentContainer.GetService(serviceType);
}
Design.ServiceCreatorCallback callback = service as Design.ServiceCreatorCallback;
if (callback != null) {
service = callback(this, serviceType);
}
return service;
}
#endregion
}
#endregion
#endif
}
}
|