Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Container / AbstractServiceContainer.cs
blobf064e59bcdd605ae352af26faafa7984afb7991d
1 // Copyright 2004-2008 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.Container
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 : IServiceContainer
27 private IServiceProvider parent;
28 private IDictionary type2Service;
30 /// <summary>
31 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
32 /// </summary>
33 public AbstractServiceContainer()
35 type2Service = new HybridDictionary();
38 /// <summary>
39 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
40 /// </summary>
41 /// <param name="parent">The parent.</param>
42 public AbstractServiceContainer(IServiceProvider parent) : this()
44 this.parent = parent;
47 #region IServiceContainer
49 /// <summary>
50 /// Adds the specified service to the service container.
51 /// </summary>
52 /// <param name="serviceType">The type of service to add.</param>
53 /// <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>
54 public void AddService(Type serviceType, object serviceInstance)
56 AddService(serviceType, serviceInstance, false);
59 /// <summary>
60 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
61 /// </summary>
62 /// <param name="serviceType">The type of service to add.</param>
63 /// <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>
64 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
65 public void AddService(Type serviceType, object serviceInstance, bool promote)
67 if (promote)
69 IServiceContainer parentContainer = ParentContainer;
71 if (parentContainer != null)
73 parentContainer.AddService(serviceType, serviceInstance, promote);
74 return;
78 type2Service[serviceType] = serviceInstance;
81 /// <summary>
82 /// Adds the specified service to the service container.
83 /// </summary>
84 /// <param name="serviceType">The type of service to add.</param>
85 /// <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>
86 public void AddService(Type serviceType, ServiceCreatorCallback callback)
88 throw new NotImplementedException();
91 /// <summary>
92 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
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 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
97 public void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
99 throw new NotImplementedException();
102 /// <summary>
103 /// Removes the specified service type from the service container.
104 /// </summary>
105 /// <param name="serviceType">The type of service to remove.</param>
106 public void RemoveService(Type serviceType)
108 RemoveService(serviceType, false);
111 /// <summary>
112 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
113 /// </summary>
114 /// <param name="serviceType">The type of service to remove.</param>
115 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
116 public void RemoveService(Type serviceType, bool promote)
118 if (promote)
120 IServiceContainer parentContainer = ParentContainer;
122 if (parentContainer != null)
124 parentContainer.RemoveService(serviceType, promote);
125 return;
129 if (type2Service != null)
131 type2Service.Remove(serviceType);
135 /// <summary>
136 /// Gets the service object of the specified type.
137 /// </summary>
138 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
139 /// <returns>
140 /// A service object of type serviceType.-or- null if there is no service object of type serviceType.
141 /// </returns>
142 public object GetService(Type serviceType)
144 object service = null;
146 if (serviceType == typeof(IServiceContainer))
148 return this;
151 if (type2Service != null)
153 service = type2Service[serviceType];
156 if (service == null && parent != null)
158 service = parent.GetService(serviceType);
161 return service;
164 #endregion
166 /// <summary>
167 /// Gets the service.
168 /// </summary>
169 /// <typeparam name="T"></typeparam>
170 /// <returns></returns>
171 public T GetService<T>() where T : class
173 return (T) GetService(typeof(T));
176 /// <summary>
177 /// Gets or sets the parent container.
178 /// </summary>
179 /// <value>The parent.</value>
180 public IServiceProvider Parent
182 get { return parent; }
183 set { parent = value; }
186 /// <summary>
187 /// Determines whether the container has the specified service.
188 /// </summary>
189 /// <typeparam name="T"></typeparam>
190 /// <returns>
191 /// <c>true</c> if this instance has service; otherwise, <c>false</c>.
192 /// </returns>
193 public bool HasService<T>() where T : class
195 return HasService(typeof(T));
198 /// <summary>
199 /// Determines whether the container has the specified service.
200 /// </summary>
201 /// <param name="serviceType">Type of the service.</param>
202 /// <returns>
203 /// <c>true</c> if the specified service type has service; otherwise, <c>false</c>.
204 /// </returns>
205 public bool HasService(Type serviceType)
207 return type2Service.Contains(serviceType);
210 /// <summary>
211 /// Adds the specified service to the service container.
212 /// </summary>
213 /// <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>
214 public void AddService<T>(object serviceInstance)
216 AddService(typeof(T), serviceInstance, false);
219 /// <summary>
220 /// Creates the specified service running all life cycles for it.
221 /// </summary>
222 /// <param name="type">The service type.</param>
223 /// <returns></returns>
224 protected virtual object CreateService(Type type)
226 throw new NotImplementedException("CreateService");
229 /// <summary>
230 /// Creates the specified service running all life cycles for it.
231 /// </summary>
232 /// <typeparam name="T">Service type</typeparam>
233 /// <returns></returns>
234 protected T CreateService<T>() where T : class, new()
236 return (T) CreateService(typeof(T));
239 /// <summary>
240 /// Gets the parent container.
241 /// </summary>
242 /// <value>The parent container.</value>
243 private IServiceContainer ParentContainer
247 IServiceContainer container = null;
249 if (parent != null)
251 container = (IServiceContainer) parent.GetService(typeof(IServiceContainer));
254 return container;