Fix the build.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / AbstractServiceContainer.cs
blob5acf314a47e7b8a6d43725c11b159474757a78e8
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 /// <summary>
23 /// Basic implementation of <see cref="IServiceContainer"/>
24 /// </summary>
25 public abstract class AbstractServiceContainer : MarshalByRefObject, IServiceContainer
27 private IServiceContainer parent;
28 private IDictionary type2Service;
30 /// <summary>
31 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
32 /// </summary>
33 public AbstractServiceContainer()
37 /// <summary>
38 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
39 /// </summary>
40 /// <param name="parent">The parent.</param>
41 public AbstractServiceContainer(IServiceContainer parent)
43 this.parent = parent;
46 #region IServiceContainer
48 /// <summary>
49 /// Adds the specified service to the service container.
50 /// </summary>
51 /// <param name="serviceType">The type of service to add.</param>
52 /// <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>
53 public void AddService(Type serviceType, object serviceInstance)
55 AddService(serviceType, serviceInstance, false);
58 /// <summary>
59 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
60 /// </summary>
61 /// <param name="serviceType">The type of service to add.</param>
62 /// <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>
63 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
64 public void AddService(Type serviceType, object serviceInstance, bool promote)
66 if (promote)
68 IServiceContainer parentContainer = ParentContainer;
70 if (parentContainer != null)
72 parentContainer.AddService(serviceType, serviceInstance, promote);
73 return;
77 if (type2Service == null)
79 type2Service = new HybridDictionary();
82 type2Service[serviceType] = serviceInstance;
85 /// <summary>
86 /// Adds the specified service to the service container.
87 /// </summary>
88 /// <param name="serviceType">The type of service to add.</param>
89 /// <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>
90 public void AddService(Type serviceType, ServiceCreatorCallback callback)
92 throw new NotImplementedException();
95 /// <summary>
96 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
97 /// </summary>
98 /// <param name="serviceType">The type of service to add.</param>
99 /// <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>
100 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
101 public void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
103 throw new NotImplementedException();
106 /// <summary>
107 /// Removes the specified service type from the service container.
108 /// </summary>
109 /// <param name="serviceType">The type of service to remove.</param>
110 public void RemoveService(Type serviceType)
112 RemoveService(serviceType, false);
115 /// <summary>
116 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
117 /// </summary>
118 /// <param name="serviceType">The type of service to remove.</param>
119 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
120 public void RemoveService(Type serviceType, bool promote)
122 if (promote)
124 IServiceContainer parentContainer = ParentContainer;
126 if (parentContainer != null)
128 parentContainer.RemoveService(serviceType, promote);
129 return;
133 if (type2Service != null)
135 type2Service.Remove(serviceType);
139 /// <summary>
140 /// Gets the service object of the specified type.
141 /// </summary>
142 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
143 /// <returns>
144 /// A service object of type serviceType.-or- null if there is no service object of type serviceType.
145 /// </returns>
146 public object GetService(Type serviceType)
148 object service = null;
150 if (serviceType == typeof(IServiceContainer))
152 return this;
155 if (type2Service != null)
157 service = type2Service[serviceType];
160 if (service == null && parent != null)
162 service = parent.GetService(serviceType);
165 return service;
168 #endregion
170 /// <summary>
171 /// Gets the service.
172 /// </summary>
173 /// <typeparam name="T"></typeparam>
174 /// <returns></returns>
175 public T GetService<T>()
177 return (T) GetService(typeof(T));
180 /// <summary>
181 /// Gets or sets the parent container.
182 /// </summary>
183 /// <value>The parent.</value>
184 public IServiceContainer Parent
186 get { return parent; }
187 set { parent = value; }
190 /// <summary>
191 /// Gets the parent container.
192 /// </summary>
193 /// <value>The parent container.</value>
194 private IServiceContainer ParentContainer
198 IServiceContainer container = null;
200 if (parent != null)
202 container = (IServiceContainer) parent.GetService(typeof(IServiceContainer));
205 return container;