2010-05-12 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / System.ComponentModel.DataAnnotations / ValidationContext.cs
1 //
2 // ValidationContext.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell Inc. (http://novell.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 using System;
31 using System.Collections.Generic;
32 using System.ComponentModel.Design;
33
34 namespace System.ComponentModel.DataAnnotations
35 {
36         public sealed class ValidationContext : IServiceProvider
37         {
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; }
44                 
45                 public ValidationContext (object instance, IServiceProvider serviceProvider, IDictionary<object, object> items)
46                 {
47                         if (instance == null)
48                                 throw new ArgumentNullException ("instance");
49                         
50                         ObjectInstance = instance;
51                         ObjectType = instance.GetType ();
52                         if (items != null)
53                                 Items = new Dictionary <object, object> (items);
54                         else
55                                 Items = new Dictionary <object, object> ();
56                         
57                         DisplayName = instance.GetType ().Name;
58
59                         // LAMESPEC: MSDN says vc.ServiceContainer should be initialized with the passed container if it implements 
60                         // the IServiceContainer interface - not the case, though.
61                         //
62                         // IServiceContainer container = serviceProvider as IServiceContainer;
63                         // if (container != null)
64                         //      ServiceContainer = container;
65                         // else
66                         ServiceContainer = new ValidationContextServiceContainer ();
67                 }
68
69                 public object GetService (Type serviceType)
70                 {
71                         return ServiceContainer.GetService (serviceType);
72                 }
73
74                 sealed class ValidationContextServiceContainer : IServiceContainer
75                 {
76                         Dictionary <Type, object> services = new Dictionary <Type, object> ();
77                         
78                         public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
79                         {
80                                 AddService (serviceType, (object)callback, promote);
81                         }
82
83                         public void AddService (Type serviceType, ServiceCreatorCallback callback)
84                         {
85                                 AddService (serviceType, callback, false);
86                         }
87
88                         public void AddService (Type serviceType, object serviceInstance, bool promote)
89                         {
90                                 if (serviceType == null)
91                                         throw new ArgumentNullException ("serviceType");
92                                 
93                                 if (services.ContainsKey (serviceType))
94                                         throw new ArgumentException (
95                                                 String.Format ("A service of type '{0}' already exists in the container.", serviceType)
96                                         );
97
98                                 services.Add (serviceType, serviceInstance);
99                         }
100
101                         public void AddService (Type serviceType, object serviceInstance)
102                         {
103                                 AddService (serviceType, serviceInstance, false);
104                         }
105
106                         public void RemoveService (Type serviceType, bool promote)
107                         {
108                                 if (serviceType == null)
109                                         throw new ArgumentNullException ("serviceType");
110                                 
111                                 if (!services.ContainsKey (serviceType))
112                                         return;
113
114                                 services.Remove (serviceType);
115                         }
116
117                         public void RemoveService (Type serviceType)
118                         {
119                                 RemoveService (serviceType, false);
120                         }
121
122                         public object GetService (Type serviceType)
123                         {
124                                 if (serviceType == null)
125                                         throw new ArgumentNullException ("serviceType");
126                                 
127                                 object o;
128                                 if (!services.TryGetValue (serviceType, out o))
129                                         return null;
130
131                                 var cb = o as ServiceCreatorCallback;
132                                 if (cb != null)
133                                         return cb (this, serviceType);
134
135                                 return o;
136                         }
137                 }
138         }
139 }