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
;
22 public abstract class AbstractServiceContainer
: MarshalByRefObject
, IGenericServiceContainer
24 private IServiceContainer parent
;
25 private IDictionary type2Service
;
28 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
30 public AbstractServiceContainer()
35 /// Initializes a new instance of the <see cref="AbstractServiceContainer"/> class.
37 /// <param name="parent">The parent.</param>
38 public AbstractServiceContainer(IServiceContainer parent
)
43 #region IGenericServiceContainer
45 public T GetService
<T
>()
47 return (T
) GetService(typeof(T
));
52 #region IServiceContainer
55 /// Adds the specified service to the service container.
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);
65 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
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
)
74 IServiceContainer parentContainer
= ParentContainer
;
76 if (parentContainer
!= null)
78 parentContainer
.AddService(serviceType
, serviceInstance
, promote
);
83 if (type2Service
== null)
85 type2Service
= new HybridDictionary();
88 type2Service
[serviceType
] = serviceInstance
;
92 /// Adds the specified service to the service container.
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();
102 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
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();
113 /// Removes the specified service type from the service container.
115 /// <param name="serviceType">The type of service to remove.</param>
116 public void RemoveService(Type serviceType
)
118 RemoveService(serviceType
, false);
122 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
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
)
130 IServiceContainer parentContainer
= ParentContainer
;
132 if (parentContainer
!= null)
134 parentContainer
.RemoveService(serviceType
, promote
);
139 if (type2Service
!= null)
141 type2Service
.Remove(serviceType
);
146 /// Gets the service object of the specified type.
148 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
150 /// A service object of type serviceType.-or- null if there is no service object of type serviceType.
152 public object GetService(Type serviceType
)
154 object service
= null;
156 if (serviceType
== typeof(IServiceContainer
))
161 if (type2Service
!= null)
163 service
= type2Service
[serviceType
];
166 if (service
== null && parent
!= null)
168 service
= parent
.GetService(serviceType
);
177 /// Gets or sets the parent container.
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;
194 container
= (IServiceContainer
) parent
.GetService(typeof(IServiceContainer
));