- If the controller cannot be found, MR searches for a special rescue "rescues/404...
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / AbstractServiceContainer.cs
blob94955b55e31737563fb4bcd28dbbbf88661d80d7
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.Services
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.ComponentModel.Design;
22 public abstract class AbstractServiceContainer : MarshalByRefObject, IGenericServiceContainer
24 private IServiceContainer parent;
25 private IDictionary type2Service;
27 /// <summary>
28 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
29 /// </summary>
30 public AbstractServiceContainer()
34 /// <summary>
35 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
36 /// </summary>
37 /// <param name="parent">The parent.</param>
38 public AbstractServiceContainer(IServiceContainer parent)
40 this.parent = parent;
43 #region IGenericServiceContainer
45 public T GetService<T>()
47 return (T) GetService(typeof(T));
50 #endregion
52 #region IServiceContainer
54 /// <summary>
55 /// Adds the specified service to the service container.
56 /// </summary>
57 /// <param name="serviceType">The type of service to add.</param>
58 /// <param name="serviceInstance">An instance of the service type to add. This object must implement or inherit from the type indicated by the serviceType parameter.</param>
59 public void AddService(Type serviceType, object serviceInstance)
61 AddService(serviceType, serviceInstance, false);
64 /// <summary>
65 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
66 /// </summary>
67 /// <param name="serviceType">The type of service to add.</param>
68 /// <param name="serviceInstance">An instance of the service type to add. This object must implement or inherit from the type indicated by the serviceType parameter.</param>
69 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
70 public void AddService(Type serviceType, object serviceInstance, bool promote)
72 if (promote)
74 IServiceContainer parentContainer = ParentContainer;
76 if (parentContainer != null)
78 parentContainer.AddService(serviceType, serviceInstance, promote);
79 return;
83 if (type2Service == null)
85 type2Service = new HybridDictionary();
88 type2Service[serviceType] = serviceInstance;
91 /// <summary>
92 /// Adds the specified service to the service container.
93 /// </summary>
94 /// <param name="serviceType">The type of service to add.</param>
95 /// <param name="callback">A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested.</param>
96 public void AddService(Type serviceType, ServiceCreatorCallback callback)
98 throw new NotImplementedException();
101 /// <summary>
102 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
103 /// </summary>
104 /// <param name="serviceType">The type of service to add.</param>
105 /// <param name="callback">A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested.</param>
106 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
107 public void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
109 throw new NotImplementedException();
112 /// <summary>
113 /// Removes the specified service type from the service container.
114 /// </summary>
115 /// <param name="serviceType">The type of service to remove.</param>
116 public void RemoveService(Type serviceType)
118 RemoveService(serviceType, false);
121 /// <summary>
122 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
123 /// </summary>
124 /// <param name="serviceType">The type of service to remove.</param>
125 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
126 public void RemoveService(Type serviceType, bool promote)
128 if (promote)
130 IServiceContainer parentContainer = ParentContainer;
132 if (parentContainer != null)
134 parentContainer.RemoveService(serviceType, promote);
135 return;
139 if (type2Service != null)
141 type2Service.Remove(serviceType);
145 /// <summary>
146 /// Gets the service object of the specified type.
147 /// </summary>
148 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
149 /// <returns>
150 /// A service object of type serviceType.-or- null if there is no service object of type serviceType.
151 /// </returns>
152 public object GetService(Type serviceType)
154 object service = null;
156 if (serviceType == typeof(IServiceContainer))
158 return this;
161 if (type2Service != null)
163 service = type2Service[serviceType];
166 if (service == null && parent != null)
168 service = parent.GetService(serviceType);
171 return service;
174 #endregion
176 /// <summary>
177 /// Gets or sets the parent container.
178 /// </summary>
179 /// <value>The parent.</value>
180 public IServiceContainer Parent
182 get { return parent; }
183 set { parent = value; }
186 private IServiceContainer ParentContainer
190 IServiceContainer container = null;
192 if (parent != null)
194 container = (IServiceContainer) parent.GetService(typeof(IServiceContainer));
197 return container;