1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
20 using System
.ComponentModel
.Design
;
23 /// Basic implementation of <see cref="IServiceContainer"/>
25 public abstract class AbstractServiceContainer
: IServiceContainer
27 private IServiceProvider parent
;
28 private IDictionary type2Service
;
31 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
33 public AbstractServiceContainer()
35 type2Service
= new HybridDictionary();
39 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
41 /// <param name="parent">The parent.</param>
42 public AbstractServiceContainer(IServiceProvider parent
) : this()
47 #region IServiceContainer
50 /// Adds the specified service to the service container.
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);
60 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
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
)
69 IServiceContainer parentContainer
= ParentContainer
;
71 if (parentContainer
!= null)
73 parentContainer
.AddService(serviceType
, serviceInstance
, promote
);
78 type2Service
[serviceType
] = serviceInstance
;
82 /// Adds the specified service to the service container.
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();
92 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
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();
103 /// Removes the specified service type from the service container.
105 /// <param name="serviceType">The type of service to remove.</param>
106 public void RemoveService(Type serviceType
)
108 RemoveService(serviceType
, false);
112 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
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
)
120 IServiceContainer parentContainer
= ParentContainer
;
122 if (parentContainer
!= null)
124 parentContainer
.RemoveService(serviceType
, promote
);
129 if (type2Service
!= null)
131 type2Service
.Remove(serviceType
);
136 /// Gets the service object of the specified type.
138 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
140 /// A service object of type serviceType.-or- null if there is no service object of type serviceType.
142 public object GetService(Type serviceType
)
144 object service
= null;
146 if (serviceType
== typeof(IServiceContainer
))
151 if (type2Service
!= null)
153 service
= type2Service
[serviceType
];
156 if (service
== null && parent
!= null)
158 service
= parent
.GetService(serviceType
);
167 /// Gets the service.
169 /// <typeparam name="T"></typeparam>
170 /// <returns></returns>
171 public T GetService
<T
>() where T
: class
173 return (T
) GetService(typeof(T
));
177 /// Gets or sets the parent container.
179 /// <value>The parent.</value>
180 public IServiceProvider Parent
182 get { return parent; }
183 set { parent = value; }
187 /// Determines whether the container has the specified service.
189 /// <typeparam name="T"></typeparam>
191 /// <c>true</c> if this instance has service; otherwise, <c>false</c>.
193 public bool HasService
<T
>() where T
: class
195 return HasService(typeof(T
));
199 /// Determines whether the container has the specified service.
201 /// <param name="serviceType">Type of the service.</param>
203 /// <c>true</c> if the specified service type has service; otherwise, <c>false</c>.
205 public bool HasService(Type serviceType
)
207 return type2Service
.Contains(serviceType
);
211 /// Adds the specified service to the service container.
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);
220 /// Creates the specified service running all life cycles for it.
222 /// <param name="type">The service type.</param>
223 /// <returns></returns>
224 protected virtual object CreateService(Type type
)
226 throw new NotImplementedException("CreateService");
230 /// Creates the specified service running all life cycles for it.
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
));
240 /// Gets the parent container.
242 /// <value>The parent container.</value>
243 private IServiceContainer ParentContainer
247 IServiceContainer container
= null;
251 container
= (IServiceContainer
) parent
.GetService(typeof(IServiceContainer
));