More working tests.
[castle.git] / InversionOfControl / Castle.MicroKernel / Registration / ComponentRegiatration.cs
bloba3abf44b06d58508a8fa89c6154da2e9630a763e
1 // Copyright 2004-2008 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.Registration
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using Castle.Core;
21 using Castle.Core.Configuration;
22 using MicroKernel;
24 /// <summary>
25 ///
26 /// </summary>
27 /// <typeparam name="S">The service type</typeparam>
28 public class ComponentRegistration<S> : IComponentRegistration
30 private String name;
31 private bool overwrite;
32 private readonly Type serviceType;
33 private Type classType;
34 private readonly List<ComponentDescriptor<S>> descriptors;
35 private ComponentModel componentModel;
37 /// <summary>
38 /// Initializes a new instance of the <see cref="ComponentRegistration{S}"/> class.
39 /// </summary>
40 public ComponentRegistration()
41 : this(typeof(S))
45 protected ComponentRegistration(Type serviceType)
47 overwrite = false;
48 this.serviceType = serviceType;
49 descriptors = new List<ComponentDescriptor<S>>();
52 internal bool Overwrite
54 get { return overwrite; }
57 /// <summary>
58 /// With the overwrite.
59 /// </summary>
60 /// <returns></returns>
61 public ComponentRegistration<S> OverWrite()
63 overwrite = true;
64 return this;
67 /// <summary>
68 /// With the name.
69 /// </summary>
70 /// <param name="name">The name.</param>
71 /// <returns></returns>
72 public ComponentRegistration<S> Named(String name)
74 if (this.name != null)
76 String message = String.Format("This component has " +
77 "already been assigned name '{0}'", this.name);
79 throw new ComponentRegistrationException(message);
82 this.name = name;
83 return this;
86 public ComponentRegistration<S> ImplementedBy<C>()
88 return ImplementedBy(typeof(C));
91 public ComponentRegistration<S> ImplementedBy(Type type)
93 if (classType != null)
95 String message = String.Format("This component has " +
96 "already been assigned implementation {0}", classType.FullName);
97 throw new ComponentRegistrationException(message);
100 classType = type;
101 return this;
104 /// <summary>
105 /// With the instance.
106 /// </summary>
107 /// <param name="instance">The instance.</param>
108 /// <returns></returns>
109 public ComponentRegistration<S> Instance(S instance)
111 return AddDescriptor(new ComponentInstanceDescriptior<S>(instance));
114 /// <summary>
115 /// Gets the proxy.
116 /// </summary>
117 /// <value>The proxy.</value>
118 public Proxy.ProxyGroup<S> Proxy
120 get { return new Proxy.ProxyGroup<S>(this); }
123 /// <summary>
124 /// Gets the with lifestyle.
125 /// </summary>
126 /// <value>The with lifestyle.</value>
127 public Lifestyle.LifestyleGroup<S> LifeStyle
129 get { return new Lifestyle.LifestyleGroup<S>(this); }
132 /// <summary>
133 /// With the activator.
134 /// </summary>
135 /// <returns></returns>
136 public ComponentRegistration<S> Activator<A>() where A : IComponentActivator
138 return AddAttributeDescriptor("componentActivatorType", typeof(A).AssemblyQualifiedName);
141 /// <summary>
142 /// With the extended properties.
143 /// </summary>
144 /// <param name="properties">The properties.</param>
145 /// <returns></returns>
146 public ComponentRegistration<S> ExtendedProperties(params Property[] properties)
148 return AddDescriptor(new ExtendedPropertiesDescriptor<S>(properties));
151 /// <summary>
152 /// With the extended properties.
153 /// </summary>
154 /// <param name="anonymous">The properties.</param>
155 /// <returns></returns>
156 public ComponentRegistration<S> ExtendedProperties(object anonymous)
158 return AddDescriptor(new ExtendedPropertiesDescriptor<S>(anonymous));
161 /// <summary>
162 /// With the custom dependencies.
163 /// </summary>
164 /// <param name="dependencies">The dependencies.</param>
165 /// <returns></returns>
166 public ComponentRegistration<S> CustomDependencies(params Property[] dependencies)
168 return AddDescriptor(new CustomDependencyDescriptor<S>(dependencies));
171 /// <summary>
172 /// With the custom dependencies.
173 /// </summary>
174 /// <param name="dependencies">The dependencies.</param>
175 /// <returns></returns>
176 public ComponentRegistration<S> CustomDependencies(IDictionary dependencies)
178 return AddDescriptor(new CustomDependencyDescriptor<S>(dependencies));
181 /// <summary>
182 /// With the custom dependencies.
183 /// </summary>
184 /// <param name="anonymous">The dependencies.</param>
185 /// <returns></returns>
186 public ComponentRegistration<S> CustomDependencies(object anonymous)
188 return AddDescriptor(new CustomDependencyDescriptor<S>(anonymous));
191 /// <summary>
192 /// With the service overrides.
193 /// </summary>
194 /// <param name="overrides">The overrides.</param>
195 /// <returns></returns>
196 public ComponentRegistration<S> ServiceOverrides(params ServiceOverride[] overrides)
198 return AddDescriptor(new ServiceOverrideDescriptor<S>(overrides));
201 /// <summary>
202 /// With the service overrides.
203 /// </summary>
204 /// <param name="overrides">The overrides.</param>
205 /// <returns></returns>
206 public ComponentRegistration<S> ServiceOverrides(IDictionary overrides)
208 return AddDescriptor(new ServiceOverrideDescriptor<S>(overrides));
211 /// <summary>
212 /// With the service overrides.
213 /// </summary>
214 /// <param name="anonymous">The overrides.</param>
215 /// <returns></returns>
216 public ComponentRegistration<S> ServiceOverrides(object anonymous)
218 return AddDescriptor(new ServiceOverrideDescriptor<S>(anonymous));
221 /// <summary>
222 /// With the interceptors.
223 /// </summary>
224 /// <param name="interceptors">The interceptors.</param>
225 /// <returns></returns>
226 public Interceptor.InterceptorGroup<S> Interceptors(
227 params InterceptorReference[] interceptors)
229 return new Interceptor.InterceptorGroup<S>(this, interceptors);
232 /// <summary>
233 /// Ases the startable.
234 /// </summary>
235 /// <returns></returns>
236 public ComponentRegistration<S> Startable()
238 return AddDescriptor(new ExtendedPropertiesDescriptor<S>(
239 Property.ForKey("startable").Eq(true)));
242 /// <summary>
243 /// Registers this component with the <see cref="IKernel"/>.
244 /// </summary>
245 /// <param name="kernel">The kernel.</param>
246 void IComponentRegistration.Register(IKernel kernel)
248 if (componentModel == null)
250 InitializeDefaults();
251 componentModel = BuildComponentModel(kernel);
252 kernel.AddCustomComponent(componentModel);
256 /// <summary>
257 /// Builds the component model.
258 /// </summary>
259 /// <returns></returns>
260 private ComponentModel BuildComponentModel(IKernel kernel)
262 IConfiguration configuration = EnsureComponentConfiguration(kernel);
263 foreach(ComponentDescriptor<S> descriptor in descriptors)
265 descriptor.ApplyToConfiguration(kernel, configuration);
268 ComponentModel model = kernel.ComponentModelBuilder.BuildModel(
269 name, serviceType, classType, null);
270 foreach(ComponentDescriptor<S> descriptor in descriptors)
272 descriptor.ApplyToModel(kernel, model);
275 return model;
278 /// <summary>
279 /// Adds the attribute descriptor.
280 /// </summary>
281 /// <param name="name">The name.</param>
282 /// <param name="value">The value.</param>
283 /// <returns></returns>
284 public ComponentRegistration<S> AddAttributeDescriptor(string name, string value)
286 AddDescriptor(new AttributeDescriptor<S>(name, value));
287 return this;
290 /// <summary>
291 /// Adds the descriptor.
292 /// </summary>
293 /// <param name="descriptor">The descriptor.</param>
294 /// <returns></returns>
295 public ComponentRegistration<S> AddDescriptor(ComponentDescriptor<S> descriptor)
297 descriptor.Registration = this;
298 descriptors.Add(descriptor);
299 return this;
302 internal void AddParameter(IKernel kernel, ComponentModel model, String name, String value)
304 IConfiguration configuration = EnsureComponentConfiguration(kernel);
305 IConfiguration parameters = configuration.Children["parameters"];
306 if (parameters == null)
308 parameters = new MutableConfiguration("component");
309 configuration.Children.Add(parameters);
312 MutableConfiguration reference = new MutableConfiguration(name, value);
313 parameters.Children.Add(reference);
314 model.Parameters.Add(name, value);
317 private void InitializeDefaults()
319 if (classType == null)
321 classType = serviceType;
324 if (String.IsNullOrEmpty(name))
326 name = classType.FullName;
330 private IConfiguration EnsureComponentConfiguration(IKernel kernel)
332 IConfiguration configuration = kernel.ConfigurationStore.GetComponentConfiguration(name);
333 if (configuration == null)
335 configuration = new MutableConfiguration("component");
336 kernel.ConfigurationStore.AddComponentConfiguration(name, configuration);
338 return configuration;
342 public class ComponentRegistration : ComponentRegistration<object>
344 public ComponentRegistration(Type serviceType)
345 : base( serviceType )