2 // ValidationContext.cs
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2010 Novell Inc. (http://novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
32 using System.ComponentModel.Design;
34 namespace System.ComponentModel.DataAnnotations
36 public sealed class ValidationContext : IServiceProvider
38 public string DisplayName { get; set; }
39 public IDictionary <object, object> Items { get; private set; }
40 public string MemberName { get; set; }
41 public object ObjectInstance { get; private set; }
42 public Type ObjectType { get; private set; }
43 public IServiceContainer ServiceContainer { get; private set; }
45 public ValidationContext (object instance, IServiceProvider serviceProvider, IDictionary<object, object> items)
48 throw new ArgumentNullException ("instance");
50 ObjectInstance = instance;
51 ObjectType = instance.GetType ();
53 Items = new Dictionary <object, object> (items);
55 Items = new Dictionary <object, object> ();
57 DisplayName = instance.GetType ().Name;
59 // LAMESPEC: MSDN says vc.ServiceContainer should be initialized with the passed container if it implements
60 // the IServiceContainer interface - not the case, though.
62 // IServiceContainer container = serviceProvider as IServiceContainer;
63 // if (container != null)
64 // ServiceContainer = container;
66 ServiceContainer = new ValidationContextServiceContainer ();
69 public object GetService (Type serviceType)
71 return ServiceContainer.GetService (serviceType);
74 sealed class ValidationContextServiceContainer : IServiceContainer
76 Dictionary <Type, object> services = new Dictionary <Type, object> ();
78 public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
80 AddService (serviceType, (object)callback, promote);
83 public void AddService (Type serviceType, ServiceCreatorCallback callback)
85 AddService (serviceType, callback, false);
88 public void AddService (Type serviceType, object serviceInstance, bool promote)
90 if (serviceType == null)
91 throw new ArgumentNullException ("serviceType");
93 if (services.ContainsKey (serviceType))
94 throw new ArgumentException (
95 String.Format ("A service of type '{0}' already exists in the container.", serviceType)
98 services.Add (serviceType, serviceInstance);
101 public void AddService (Type serviceType, object serviceInstance)
103 AddService (serviceType, serviceInstance, false);
106 public void RemoveService (Type serviceType, bool promote)
108 if (serviceType == null)
109 throw new ArgumentNullException ("serviceType");
111 if (!services.ContainsKey (serviceType))
114 services.Remove (serviceType);
117 public void RemoveService (Type serviceType)
119 RemoveService (serviceType, false);
122 public object GetService (Type serviceType)
124 if (serviceType == null)
125 throw new ArgumentNullException ("serviceType");
128 if (!services.TryGetValue (serviceType, out o))
131 var cb = o as ServiceCreatorCallback;
133 return cb (this, serviceType);