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 ();
70 public ValidationContext (object instance)
71 : this (instance, null, null)
75 public ValidationContext (object instance, IDictionary<object, object> items)
76 : this (instance, null, items)
80 // FIXME: According to MSDN, this should be defined in
81 // 4.5, Silverlight and PCL
83 public void InitializeServiceProvider (
84 Func<Type, Object> serviceProvider)
86 throw new NotImplementedException ();
90 public object GetService (Type serviceType)
92 return ServiceContainer.GetService (serviceType);
95 sealed class ValidationContextServiceContainer : IServiceContainer
97 Dictionary <Type, object> services = new Dictionary <Type, object> ();
99 public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
101 AddService (serviceType, (object)callback, promote);
104 public void AddService (Type serviceType, ServiceCreatorCallback callback)
106 AddService (serviceType, callback, false);
109 public void AddService (Type serviceType, object serviceInstance, bool promote)
111 if (serviceType == null)
112 throw new ArgumentNullException ("serviceType");
114 if (services.ContainsKey (serviceType))
115 throw new ArgumentException (
116 String.Format ("A service of type '{0}' already exists in the container.", serviceType)
119 services.Add (serviceType, serviceInstance);
122 public void AddService (Type serviceType, object serviceInstance)
124 AddService (serviceType, serviceInstance, false);
127 public void RemoveService (Type serviceType, bool promote)
129 if (serviceType == null)
130 throw new ArgumentNullException ("serviceType");
132 if (!services.ContainsKey (serviceType))
135 services.Remove (serviceType);
138 public void RemoveService (Type serviceType)
140 RemoveService (serviceType, false);
143 public object GetService (Type serviceType)
145 if (serviceType == null)
146 throw new ArgumentNullException ("serviceType");
149 if (!services.TryGetValue (serviceType, out o))
152 var cb = o as ServiceCreatorCallback;
154 return cb (this, serviceType);