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
.MicroKernel
18 using System
.Collections
;
20 using Castle
.MicroKernel
.Registration
;
23 /// The <c>IKernel</c> interface exposes all the functionality
24 /// the MicroKernel implements.
27 /// It allows you to register components and
28 /// request them by the key or the service they implemented.
29 /// It also allow you to register facilities and subsystem, thus
30 /// augmenting the functionality exposed by the kernel alone to fits
32 /// <seealso cref="IFacility"/>
33 /// <seealso cref="ISubSystem"/>
35 public interface IKernel
: IServiceProviderEx
, IKernelEvents
, IDisposable
38 /// Adds a concrete class as a component
40 /// <param name="key"></param>
41 /// <param name="classType"></param>
42 void AddComponent(String key
, Type classType
);
45 /// Adds a concrete class
46 /// as a component with the specified <paramref name="lifestyle"/>.
48 /// <param name="key">The key with which to index the component.</param>
49 /// <param name="classType">The <see cref="Type"/> of the component.</param>
50 /// <param name="lifestyle">The specified <see cref="LifestyleType"/> for the component.</param>
52 /// If you have indicated a lifestyle for the specified <paramref name="classType"/> using
53 /// attributes, this method will not overwrite that lifestyle. To do that, use the
54 /// <see cref="AddComponent(string,Type,LifestyleType,bool)"/> method.
56 /// <exception cref="ArgumentNullException">
57 /// Thrown if <paramref name="key"/>, or <paramref name="classType"/>
58 /// are <see langword="null"/>.
60 /// <exception cref="ArgumentException">
61 /// Thrown if <paramref name="lifestyle"/> is <see cref="LifestyleType.Undefined"/>.
63 void AddComponent(String key
, Type classType
, LifestyleType lifestyle
);
66 /// Adds a concrete class
67 /// as a component with the specified <paramref name="lifestyle"/>.
69 /// <param name="key">The key with which to index the component.</param>
70 /// <param name="classType">The <see cref="Type"/> of the component.</param>
71 /// <param name="lifestyle">The specified <see cref="LifestyleType"/> for the component.</param>
72 /// <param name="overwriteLifestyle">
73 /// If <see langword="true"/>, then ignores all other configurations
74 /// for lifestyle and uses the value in the <paramref name="lifestyle"/> parameter.
77 /// If you have indicated a lifestyle for the specified <paramref name="classType"/> using
78 /// attributes, this method will not overwrite that lifestyle. To do that, use the
79 /// <see cref="AddComponent(string,Type,Type,LifestyleType,bool)"/> method.
81 /// <exception cref="ArgumentNullException">
82 /// Thrown if <paramref name="key"/> or <paramref name="classType"/>
83 /// are <see langword="null"/>.
85 /// <exception cref="ArgumentException" />
86 /// Thrown if <paramref name="lifestyle"/> is <see cref="LifestyleType.Undefined"/>.
87 void AddComponent(String key
, Type classType
, LifestyleType lifestyle
, bool overwriteLifestyle
);
90 /// Adds a concrete class and an interface
93 /// <param name="key">The key with which to index the component.</param>
94 /// <param name="serviceType">The service <see cref="Type"/> that this component implements.</param>
95 /// <param name="classType">The <see cref="Type"/> of the component.</param>
96 void AddComponent(String key
, Type serviceType
, Type classType
);
99 /// Adds a concrete class and an interface
100 /// as a component with the specified <paramref name="lifestyle"/>.
102 /// <param name="key">The key with which to index the component.</param>
103 /// <param name="serviceType">The service <see cref="Type"/> that this component implements.</param>
104 /// <param name="classType">The <see cref="Type"/> of the component.</param>
105 /// <param name="lifestyle">The specified <see cref="LifestyleType"/> for the component.</param>
107 /// If you have indicated a lifestyle for the specified <paramref name="classType"/> using
108 /// attributes, this method will not overwrite that lifestyle. To do that, use the
109 /// <see cref="AddComponent(string,Type,Type,LifestyleType,bool)"/> method.
111 /// <exception cref="ArgumentNullException">
112 /// Thrown if <paramref name="key"/>, <paramref name="serviceType"/>, or <paramref name="classType"/>
113 /// are <see langword="null"/>.
115 /// <exception cref="ArgumentException">
116 /// Thrown if <paramref name="lifestyle"/> is <see cref="LifestyleType.Undefined"/>.
118 void AddComponent(String key
, Type serviceType
, Type classType
, LifestyleType lifestyle
);
121 /// Adds a concrete class and an interface
122 /// as a component with the specified <paramref name="lifestyle"/>.
124 /// <param name="key">The key with which to index the component.</param>
125 /// <param name="serviceType">The service <see cref="Type"/> that this component implements.</param>
126 /// <param name="classType">The <see cref="Type"/> of the component.</param>
127 /// <param name="lifestyle">The specified <see cref="LifestyleType"/> for the component.</param>
128 /// <param name="overwriteLifestyle">
129 /// If <see langword="true"/>, then ignores all other configurations
130 /// for lifestyle and uses the value in the <paramref name="lifestyle"/> parameter.
133 /// If you have indicated a lifestyle for the specified <paramref name="classType"/> using
134 /// attributes, this method will not overwrite that lifestyle. To do that, use the
135 /// <see cref="AddComponent(string,Type,Type,LifestyleType,bool)"/> method.
137 /// <exception cref="ArgumentNullException">
138 /// Thrown if <paramref name="key"/>, <paramref name="serviceType"/>, or <paramref name="classType"/>
139 /// are <see langword="null"/>.
141 /// <exception cref="ArgumentException">
142 /// Thrown if <paramref name="lifestyle"/> is <see cref="LifestyleType.Undefined"/>.
144 void AddComponent(string key
, Type serviceType
, Type classType
, LifestyleType lifestyle
, bool overwriteLifestyle
);
147 /// Adds a concrete class as a component
149 void AddComponent
<T
>();
152 /// Adds a concrete class
153 /// as a component with the specified <paramref name="lifestyle"/>.
155 /// <param name="lifestyle">The specified <see cref="LifestyleType"/> for the component.</param>
157 /// If you have indicated a lifestyle for the specified T using
158 /// attributes, this method will not overwrite that lifestyle. To do that, use the
159 /// <see cref="AddComponent(string,Type,LifestyleType,bool)"/> method.
161 /// <exception cref="ArgumentException">
162 /// Thrown if <paramref name="lifestyle"/> is <see cref="LifestyleType.Undefined"/>.
164 void AddComponent
<T
>(LifestyleType lifestyle
);
167 /// Adds a concrete class
168 /// as a component with the specified <paramref name="lifestyle"/>.
170 /// <param name="lifestyle">The specified <see cref="LifestyleType"/> for the component.</param>
171 /// <param name="overwriteLifestyle">
172 /// If <see langword="true"/>, then ignores all other configurations
173 /// for lifestyle and uses the value in the <paramref name="lifestyle"/> parameter.
176 /// If you have indicated a lifestyle for the specified T using
177 /// attributes, this method will not overwrite that lifestyle. To do that, use the
178 /// <see cref="AddComponent(string,Type,LifestyleType,bool)"/> method.
180 /// <exception cref="ArgumentException" />
181 /// Thrown if <paramref name="lifestyle"/> is <see cref="LifestyleType.Undefined"/>.
182 void AddComponent
<T
>(LifestyleType lifestyle
, bool overwriteLifestyle
);
185 /// Adds a concrete class and an interface
188 /// <param name="serviceType">The service <see cref="Type"/> that this component implements.</param>
189 void AddComponent
<T
>(Type serviceType
);
192 /// Adds a concrete class and an interface
193 /// as a component with the specified <paramref name="lifestyle"/>.
195 /// <param name="serviceType">The service <see cref="Type"/> that this component implements.</param>
196 /// <param name="lifestyle">The specified <see cref="LifestyleType"/> for the component.</param>
198 /// If you have indicated a lifestyle for the specified T using
199 /// attributes, this method will not overwrite that lifestyle. To do that, use the
200 /// <see cref="AddComponent(string,Type,Type,LifestyleType,bool)"/> method.
202 /// <exception cref="ArgumentNullException">
203 /// are <see langword="null"/>.
205 /// <exception cref="ArgumentException">
206 /// Thrown if <paramref name="lifestyle"/> is <see cref="LifestyleType.Undefined"/>.
208 void AddComponent
<T
>(Type serviceType
, LifestyleType lifestyle
);
211 /// Adds a concrete class and an interface
212 /// as a component with the specified <paramref name="lifestyle"/>.
214 /// <param name="serviceType">The service <see cref="Type"/> that this component implements.</param>
215 /// <param name="lifestyle">The specified <see cref="LifestyleType"/> for the component.</param>
216 /// <param name="overwriteLifestyle">
217 /// If <see langword="true"/>, then ignores all other configurations
218 /// for lifestyle and uses the value in the <paramref name="lifestyle"/> parameter.
221 /// attributes, this method will not overwrite that lifestyle. To do that, use the
222 /// <see cref="AddComponent(string,Type,Type,LifestyleType,bool)"/> method.
224 /// <exception cref="ArgumentNullException">
225 /// are <see langword="null"/>.
227 /// <exception cref="ArgumentException">
228 /// Thrown if <paramref name="lifestyle"/> is <see cref="LifestyleType.Undefined"/>.
230 void AddComponent
<T
>(Type serviceType
, LifestyleType lifestyle
, bool overwriteLifestyle
);
233 /// Used mostly by facilities. Adds an instance
234 /// to be used as a component.
236 /// <param name="instance"></param>
237 void AddComponentInstance
<T
>(object instance
);
240 /// Used mostly by facilities. Adds an instance
241 /// to be used as a component.
243 /// <param name="serviceType"></param>
244 /// <param name="instance"></param>
245 void AddComponentInstance
<T
>(Type serviceType
, object instance
);
248 /// Adds a concrete class as a component and specify the extended properties.
249 /// Used by facilities, mostly.
251 /// <param name="key"></param>
252 /// <param name="classType"></param>
253 /// <param name="extendedProperties"></param>
254 void AddComponentWithExtendedProperties(String key
, Type classType
, IDictionary extendedProperties
);
257 /// Adds a concrete class and an interface
258 /// as a component and specify the extended properties.
259 /// Used by facilities, mostly.
261 /// <param name="key"></param>
262 /// <param name="serviceType"></param>
263 /// <param name="classType"></param>
264 /// <param name="extendedProperties"></param>
265 void AddComponentWithExtendedProperties(String key
, Type serviceType
, Type classType
, IDictionary extendedProperties
);
268 /// Adds a custom made <see cref="ComponentModel"/>.
269 /// Used by facilities.
271 /// <param name="model"></param>
272 void AddCustomComponent(ComponentModel model
);
275 /// Used mostly by facilities. Adds an instance
276 /// to be used as a component.
278 /// <param name="key"></param>
279 /// <param name="instance"></param>
280 void AddComponentInstance(String key
, object instance
);
283 /// Used mostly by facilities. Adds an instance
284 /// to be used as a component.
286 /// <param name="key"></param>
287 /// <param name="serviceType"></param>
288 /// <param name="instance"></param>
289 void AddComponentInstance(String key
, Type serviceType
, object instance
);
292 /// Used mostly by facilities. Adds an instance
293 /// to be used as a component.
295 /// <param name="key"></param>
296 /// <param name="serviceType"></param>
297 /// <param name="instance"></param>
298 /// <param name="classType"></param>
299 void AddComponentInstance(string key
, Type serviceType
, Type classType
, object instance
);
302 /// Adds a component to be registered with the <see cref="IKernel"/>
303 /// using a fluent interface.
305 /// <typeparam name="S">The service <see cref="Type"/> to manage.</typeparam>
306 /// <returns>The <see cref="ComponentRegistration{S,T}"/></returns>
307 ComponentRegistration
<S
, IKernel
> AddComponentEx
<S
>();
310 /// Returns true if the specified component was
311 /// found and could be removed (i.e. no other component depends on it)
313 /// <param name="key">The component's key</param>
314 /// <returns></returns>
315 bool RemoveComponent(String key
);
318 /// Returns true if the specified key was registered
320 /// <param name="key"></param>
321 /// <returns></returns>
322 bool HasComponent(String key
);
325 /// Returns true if the specified service was registered
327 /// <param name="service"></param>
328 /// <returns></returns>
329 bool HasComponent(Type service
);
332 /// Returns the component instance by the key
334 object this[String key
] { get; }
337 /// Returns the component instance by the service type
339 object this[Type service
] { get; }
342 /// Returns the component instance by the service type
344 object Resolve(Type service
);
347 /// Returns all the valid component instances by
350 /// <param name="service">The service type</param>
351 /// <param name="arguments">Arguments to resolve the services</param>
352 Array
ResolveAll(Type service
, IDictionary arguments
);
355 /// Returns the component instance by the service type
356 /// using dynamic arguments
358 /// <param name="service"></param>
359 /// <param name="arguments"></param>
360 /// <returns></returns>
361 object Resolve(Type service
, IDictionary arguments
);
364 /// Returns the component instance by the component key
365 /// using dynamic arguments
367 /// <param name="key"></param>
368 /// <param name="arguments"></param>
369 /// <returns></returns>
370 object Resolve(String key
, IDictionary arguments
);
373 /// Returns a component instance by the key
375 /// <param name="key"></param>
376 /// <param name="service"></param>
377 /// <returns></returns>
378 object Resolve(String key
, Type service
);
381 /// Returns the component instance by the service type
382 /// using dynamic arguments
384 /// <param name="arguments"></param>
385 /// <returns></returns>
386 T Resolve
<T
>(IDictionary arguments
);
389 /// Returns the component instance by the component key
391 /// <returns></returns>
395 /// Returns component instances that implement TService
397 /// <typeparam name="TService"></typeparam>
398 /// <returns></returns>
399 TService
[] ResolveServices
<TService
>();
402 /// Returns a component instance by the key
404 /// <param name="key"></param>
405 /// <param name="service"></param>
406 /// <param name="arguments"></param>
407 /// <returns></returns>
408 object Resolve(String key
, Type service
, IDictionary arguments
);
411 /// Associates objects with a component handler,
412 /// allowing it to use the specified dictionary
413 /// when resolving dependencies
415 /// <param name="service"></param>
416 /// <param name="dependencies"></param>
417 void RegisterCustomDependencies(Type service
, IDictionary dependencies
);
420 /// Associates objects with a component handler,
421 /// allowing it to use the specified dictionary
422 /// when resolving dependencies
424 /// <param name="key"></param>
425 /// <param name="dependencies"></param>
426 void RegisterCustomDependencies(String key
, IDictionary dependencies
);
429 /// Releases a component instance. This allows
430 /// the kernel to execute the proper decomission
431 /// lifecycles on the component instance.
433 /// <param name="instance"></param>
434 void ReleaseComponent(object instance
);
437 /// Constructs an implementation of <see cref="IComponentActivator"/>
438 /// for the given <see cref="ComponentModel"/>
440 /// <param name="model"></param>
441 /// <returns></returns>
442 IComponentActivator
CreateComponentActivator(ComponentModel model
);
445 /// Returns the implementation of <see cref="IComponentModelBuilder"/>
447 IComponentModelBuilder ComponentModelBuilder { get; }
450 /// Returns the implementation of <see cref="IHandlerFactory"/>
452 IHandlerFactory HandlerFactory { get; }
455 /// Gets or sets the implementation of <see cref="IConfigurationStore"/>
457 IConfigurationStore ConfigurationStore { get; set; }
460 /// Returns the <see cref="IHandler"/>
461 /// for the specified component key.
463 /// <param name="key"></param>
464 /// <returns></returns>
465 IHandler
GetHandler(String key
);
468 /// Returns the <see cref="IHandler"/>
469 /// for the specified service.
471 /// <param name="service"></param>
472 /// <returns></returns>
473 IHandler
GetHandler(Type service
);
476 /// Return handlers for components that
477 /// implements the specified service.
479 /// <param name="service"></param>
480 /// <returns></returns>
481 IHandler
[] GetHandlers(Type service
);
484 /// Return handlers for components that
485 /// implements the specified service.
486 /// The check is made using IsAssignableFrom
488 /// <param name="service"></param>
489 /// <returns></returns>
490 IHandler
[] GetAssignableHandlers(Type service
);
493 /// Gets or sets the implementation for <see cref="IReleasePolicy"/>
495 IReleasePolicy ReleasePolicy { get; set; }
498 /// Returns the implementation for <see cref="IDependencyResolver"/>
500 IDependencyResolver Resolver { get; }
503 /// Adds a <see cref="IFacility"/> to the kernel.
505 /// <param name="key"></param>
506 /// <param name="facility"></param>
507 void AddFacility(String key
, IFacility facility
);
510 /// Returns the facilities registered on the kernel.
512 /// <returns></returns>
513 IFacility
[] GetFacilities();
516 /// Adds (or replaces) an <see cref="ISubSystem"/>
518 /// <param name="key"></param>
519 /// <param name="subsystem"></param>
520 void AddSubSystem(String key
, ISubSystem subsystem
);
523 /// Returns an implementation of <see cref="ISubSystem"/>
524 /// for the specified key.
525 /// <seealso cref="SubSystemConstants"/>
527 /// <param name="key"></param>
528 /// <returns></returns>
529 ISubSystem
GetSubSystem(String key
);
532 /// Gets or sets the implementation of <see cref="IProxyFactory"/>
533 /// allowing different strategies for proxy creation.
535 IProxyFactory ProxyFactory { get; set; }
538 /// Returns the parent kernel
540 IKernel Parent { get; set; }
543 /// Support for kernel hierarchy
545 /// <param name="kernel"></param>
546 void AddChildKernel(IKernel kernel
);
549 /// Remove child kernel
551 /// <param name="kernel"></param>
552 void RemoveChildKernel(IKernel kernel
);
555 /// Graph of components and iteractions.
557 GraphNode
[] GraphNodes { get; }
560 /// Raise the hanlder registered event, required so
561 /// dependant handlers will be notified about their dependant moving
564 /// <param name="handler"></param>
565 void RaiseHandlerRegistered(IHandler handler
);