Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel / ModelBuilder / DefaultComponentModelBuilder.cs
blob9a699615ff90c35faa70a1aa8b555f2823ec9803
1 // Copyright 2004-2007 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.MicroKernel.ModelBuilder
17 using System;
18 using System.Collections;
20 using Castle.Core;
21 using Castle.MicroKernel.ModelBuilder.Inspectors;
23 /// <summary>
24 /// Summary description for DefaultComponentModelBuilder.
25 /// </summary>
26 [Serializable]
27 public class DefaultComponentModelBuilder : IComponentModelBuilder
29 private readonly IKernel kernel;
30 private readonly ArrayList contributors;
32 /// <summary>
33 /// Initializes a new instance of the <see cref="DefaultComponentModelBuilder"/> class.
34 /// </summary>
35 /// <param name="kernel">The kernel.</param>
36 public DefaultComponentModelBuilder(IKernel kernel)
38 this.kernel = kernel;
39 contributors = new ArrayList();
41 InitializeContributors();
44 /// <summary>
45 /// Constructs a new ComponentModel by invoking
46 /// the registered contributors.
47 /// </summary>
48 /// <param name="key"></param>
49 /// <param name="service"></param>
50 /// <param name="classType"></param>
51 /// <param name="extendedProperties"></param>
52 /// <returns></returns>
53 public ComponentModel BuildModel(String key, Type service, Type classType, IDictionary extendedProperties)
55 ComponentModel model = new ComponentModel(key, service, classType);
57 if (extendedProperties != null)
59 model.ExtendedProperties = extendedProperties;
62 foreach(IContributeComponentModelConstruction contributor in contributors)
64 contributor.ProcessModel( kernel, model );
67 return model;
70 /// <summary>
71 /// Gets the contributors.
72 /// </summary>
73 /// <value>The contributors.</value>
74 public IContributeComponentModelConstruction[] Contributors
76 get
78 return (IContributeComponentModelConstruction[])
79 contributors.ToArray(typeof(IContributeComponentModelConstruction));
83 /// <summary>
84 /// "To give or supply in common with others; give to a
85 /// common fund or for a common purpose". The contributor
86 /// should inspect the component, or even the configuration
87 /// associated with the component, to add or change information
88 /// in the model that can be used later.
89 /// </summary>
90 /// <param name="contributor"></param>
91 public void AddContributor(IContributeComponentModelConstruction contributor)
93 contributors.Add(contributor);
96 /// <summary>
97 /// Removes the specified contributor
98 /// </summary>
99 /// <param name="contributor"></param>
100 public void RemoveContributor(IContributeComponentModelConstruction contributor)
102 contributors.Remove(contributor);
105 /// <summary>
106 /// Initializes the default contributors.
107 /// </summary>
108 protected virtual void InitializeContributors()
110 AddContributor(new GenericInspector());
111 AddContributor(new ConfigurationModelInspector());
112 AddContributor(new LifestyleModelInspector());
113 AddContributor(new ConstructorDependenciesModelInspector());
114 AddContributor(new PropertiesDependenciesModelInspector());
115 AddContributor(new LifecycleModelInspector());
116 AddContributor(new ConfigurationParametersInspector());
117 AddContributor(new InterceptorInspector());
118 AddContributor(new ComponentActivatorInspector());
119 AddContributor(new ComponentProxyInspector());