1 // Copyright 2004-2007 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
.Services
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
: MarshalByRefObject
, IServiceContainer
27 private IServiceContainer parent
;
28 private IDictionary type2Service
;
31 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
33 public AbstractServiceContainer()
38 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
40 /// <param name="parent">The parent.</param>
41 public AbstractServiceContainer(IServiceContainer parent
)
46 #region IServiceContainer
49 /// Adds the specified service to the service container.
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);
59 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
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
)
68 IServiceContainer parentContainer
= ParentContainer
;
70 if (parentContainer
!= null)
72 parentContainer
.AddService(serviceType
, serviceInstance
, promote
);
77 if (type2Service
== null)
79 type2Service
= new HybridDictionary();
82 type2Service
[serviceType
] = serviceInstance
;
86 /// Adds the specified service to the service container.
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();
96 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
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();
107 /// Removes the specified service type from the service container.
109 /// <param name="serviceType">The type of service to remove.</param>
110 public void RemoveService(Type serviceType
)
112 RemoveService(serviceType
, false);
116 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
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
)
124 IServiceContainer parentContainer
= ParentContainer
;
126 if (parentContainer
!= null)
128 parentContainer
.RemoveService(serviceType
, promote
);
133 if (type2Service
!= null)
135 type2Service
.Remove(serviceType
);
140 /// Gets the service object of the specified type.
142 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
144 /// A service object of type serviceType.-or- null if there is no service object of type serviceType.
146 public object GetService(Type serviceType
)
148 object service
= null;
150 if (serviceType
== typeof(IServiceContainer
))
155 if (type2Service
!= null)
157 service
= type2Service
[serviceType
];
160 if (service
== null && parent
!= null)
162 service
= parent
.GetService(serviceType
);
171 /// Gets the service.
173 /// <typeparam name="T"></typeparam>
174 /// <returns></returns>
175 public T GetService
<T
>()
177 return (T
) GetService(typeof(T
));
181 /// Gets or sets the parent container.
183 /// <value>The parent.</value>
184 public IServiceContainer Parent
186 get { return parent; }
187 set { parent = value; }
191 /// Gets the parent container.
193 /// <value>The parent container.</value>
194 private IServiceContainer ParentContainer
198 IServiceContainer container
= null;
202 container
= (IServiceContainer
) parent
.GetService(typeof(IServiceContainer
));