Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / Configuration / ServiceEntryCollection.cs
blobd4235150086321e3f7e207b37684eafe36722f21
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.MonoRail.Framework.Configuration
17 using System;
18 using System.Collections;
19 using System.Xml;
21 using Castle.Components.Common.EmailSender;
22 using Castle.Components.Validator;
23 using Castle.MonoRail.Framework.Internal;
24 using Castle.MonoRail.Framework.Services.AjaxProxyGenerator;
26 /// <summary>
27 /// Represents a set of MonoRail services entries
28 /// </summary>
29 public class ServiceEntryCollection : ISerializedConfig
31 private Hashtable service2Impl = new Hashtable();
32 private IList customServices = new ArrayList();
34 /// <summary>
35 /// Initializes a new instance of the <see cref="ServiceEntryCollection"/> class.
36 /// </summary>
37 public ServiceEntryCollection()
41 /// <summary>
42 /// Gets the custom services.
43 /// </summary>
44 /// <value>The custom services.</value>
45 public ICollection CustomServices
47 get { return customServices; }
50 #region ISerializedConfig implementation
52 /// <summary>
53 /// Deserializes the specified section.
54 /// </summary>
55 /// <param name="section">The section.</param>
56 public void Deserialize(XmlNode section)
58 XmlNodeList services = section.SelectNodes("services/service");
60 foreach(XmlNode node in services)
62 ServiceEntry entry = new ServiceEntry();
64 entry.Deserialize(node);
66 if (entry.ServiceType == ServiceIdentification.Custom)
68 if (entry.Interface != null)
70 RegisterService(entry.Interface, entry.Service);
72 else
74 customServices.Add(entry.Service);
77 else
79 RegisterService(entry.ServiceType, entry.Service);
84 #endregion
86 /// <summary>
87 /// Registers the service.
88 /// </summary>
89 /// <param name="id">The id.</param>
90 /// <param name="service">The service.</param>
91 public void RegisterService(ServiceIdentification id, Type service)
93 RegisterService(ToInterface(id), service);
96 /// <summary>
97 /// Registers the service.
98 /// </summary>
99 /// <param name="inter">The inter.</param>
100 /// <param name="service">The service.</param>
101 public void RegisterService(Type inter, Type service)
103 service2Impl[inter] = service;
106 /// <summary>
107 /// Gets the service.
108 /// </summary>
109 /// <param name="id">The id.</param>
110 /// <returns></returns>
111 public Type GetService(ServiceIdentification id)
113 return (Type) service2Impl[ToInterface(id)];
116 /// <summary>
117 /// Determines whether it has service.
118 /// </summary>
119 /// <param name="id">The id.</param>
120 /// <returns>
121 /// <c>true</c> if the specified id has service; otherwise, <c>false</c>.
122 /// </returns>
123 public bool HasService(ServiceIdentification id)
125 return service2Impl.Contains(ToInterface(id));
128 /// <summary>
129 /// Gets the service impl map.
130 /// </summary>
131 /// <value>The service impl map.</value>
132 public IDictionary ServiceImplMap
134 get { return service2Impl; }
137 private Type ToInterface(ServiceIdentification id)
139 switch(id)
141 case ServiceIdentification.ControllerFactory:
142 return typeof(IControllerFactory);
143 case ServiceIdentification.ViewComponentFactory:
144 return typeof(IViewComponentFactory);
145 case ServiceIdentification.FilterFactory:
146 return typeof(IFilterFactory);
147 case ServiceIdentification.EmailSender:
148 return typeof(IEmailSender);
149 case ServiceIdentification.ControllerDescriptorProvider:
150 return typeof(IControllerDescriptorProvider);
151 case ServiceIdentification.ResourceDescriptorProvider:
152 return typeof(IResourceDescriptorProvider);
153 case ServiceIdentification.RescueDescriptorProvider:
154 return typeof(IRescueDescriptorProvider);
155 case ServiceIdentification.LayoutDescriptorProvider:
156 return typeof(ILayoutDescriptorProvider);
157 case ServiceIdentification.HelperDescriptorProvider:
158 return typeof(IHelperDescriptorProvider);
159 case ServiceIdentification.FilterDescriptorProvider:
160 return typeof(IFilterDescriptorProvider);
161 case ServiceIdentification.EmailTemplateService:
162 return typeof(IEmailTemplateService);
163 case ServiceIdentification.ControllerTree:
164 return typeof(IControllerTree);
165 case ServiceIdentification.CacheProvider:
166 return typeof(ICacheProvider);
167 case ServiceIdentification.ViewSourceLoader:
168 return typeof(IViewSourceLoader);
169 case ServiceIdentification.ScaffoldingSupport:
170 return typeof(IScaffoldingSupport);
171 case ServiceIdentification.ViewEngineManager:
172 return typeof(IViewEngineManager);
173 case ServiceIdentification.ResourceFactory:
174 return typeof(IResourceFactory);
175 case ServiceIdentification.ExecutorFactory:
176 return typeof(IControllerLifecycleExecutorFactory);
177 case ServiceIdentification.TransformationFilterFactory:
178 return typeof(ITransformFilterFactory);
179 case ServiceIdentification.TransformFilterDescriptorProvider:
180 return typeof(ITransformFilterDescriptorProvider);
181 case ServiceIdentification.UrlBuilder:
182 return typeof(IUrlBuilder);
183 case ServiceIdentification.UrlTokenizer:
184 return typeof(IUrlTokenizer);
185 case ServiceIdentification.ServerUtility:
186 return typeof(IServerUtility);
187 case ServiceIdentification.ValidatorRegistry:
188 return typeof(IValidatorRegistry);
189 case ServiceIdentification.AjaxProxyGenerator:
190 return typeof(IAjaxProxyGenerator);
191 default:
192 throw new NotSupportedException("Id not supported " + id);