1 // Copyright 2004-2008 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
.Registration
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
21 using Castle
.Core
.Configuration
;
27 /// <typeparam name="S">The service type</typeparam>
28 public class ComponentRegistration
<S
> : IRegistration
31 private bool overwrite
;
32 private Type serviceType
;
33 private Type implementation
;
34 private readonly List
<ComponentDescriptor
<S
>> descriptors
;
35 private ComponentModel componentModel
;
38 /// Initializes a new instance of the <see cref="ComponentRegistration{S}"/> class.
40 public ComponentRegistration()
43 serviceType
= typeof(S
);
44 descriptors
= new List
<ComponentDescriptor
<S
>>();
52 public Type ServiceType
54 get { return serviceType; }
55 protected set { serviceType = value; }
58 public Type Implementation
60 get { return implementation; }
63 internal bool IsOverWrite
65 get { return overwrite; }
69 /// With the overwrite.
71 /// <returns></returns>
72 public ComponentRegistration
<S
> OverWrite()
81 /// <param name="name">The name.</param>
82 /// <returns></returns>
83 public ComponentRegistration
<S
> Named(String name
)
85 if (this.name
!= null)
87 String message
= String
.Format("This component has " +
88 "already been assigned name '{0}'", this.name
);
90 throw new ComponentRegistrationException(message
);
97 public ComponentRegistration
<S
> ImplementedBy
<C
>()
99 return ImplementedBy(typeof(C
));
102 public ComponentRegistration
<S
> ImplementedBy(Type type
)
104 if (implementation
!= null)
106 String message
= String
.Format("This component has " +
107 "already been assigned implementation {0}", implementation
.FullName
);
108 throw new ComponentRegistrationException(message
);
111 implementation
= type
;
116 /// With the instance.
118 /// <param name="instance">The instance.</param>
119 /// <returns></returns>
120 public ComponentRegistration
<S
> Instance(S instance
)
122 return AddDescriptor(new ComponentInstanceDescriptior
<S
>(instance
));
128 /// <value>The proxy.</value>
129 public Proxy
.ProxyGroup
<S
> Proxy
131 get { return new Proxy.ProxyGroup<S>(this); }
135 /// Gets the with lifestyle.
137 /// <value>The with lifestyle.</value>
138 public Lifestyle
.LifestyleGroup
<S
> LifeStyle
140 get { return new Lifestyle.LifestyleGroup<S>(this); }
144 /// With the activator.
146 /// <returns></returns>
147 public ComponentRegistration
<S
> Activator
<A
>() where A
: IComponentActivator
149 return AddAttributeDescriptor("componentActivatorType", typeof(A
).AssemblyQualifiedName
);
153 /// With the extended properties.
155 /// <param name="properties">The properties.</param>
156 /// <returns></returns>
157 public ComponentRegistration
<S
> ExtendedProperties(params Property
[] properties
)
159 return AddDescriptor(new ExtendedPropertiesDescriptor
<S
>(properties
));
163 /// With the extended properties.
165 /// <param name="anonymous">The properties.</param>
166 /// <returns></returns>
167 public ComponentRegistration
<S
> ExtendedProperties(object anonymous
)
169 return AddDescriptor(new ExtendedPropertiesDescriptor
<S
>(anonymous
));
173 /// With the custom dependencies.
175 /// <param name="dependencies">The dependencies.</param>
176 /// <returns></returns>
177 public ComponentRegistration
<S
> CustomDependencies(params Property
[] dependencies
)
179 return AddDescriptor(new CustomDependencyDescriptor
<S
>(dependencies
));
183 /// With the custom dependencies.
185 /// <param name="dependencies">The dependencies.</param>
186 /// <returns></returns>
187 public ComponentRegistration
<S
> CustomDependencies(IDictionary dependencies
)
189 return AddDescriptor(new CustomDependencyDescriptor
<S
>(dependencies
));
193 /// With the custom dependencies.
195 /// <param name="anonymous">The dependencies.</param>
196 /// <returns></returns>
197 public ComponentRegistration
<S
> CustomDependencies(object anonymous
)
199 return AddDescriptor(new CustomDependencyDescriptor
<S
>(anonymous
));
203 /// With the service overrides.
205 /// <param name="overrides">The overrides.</param>
206 /// <returns></returns>
207 public ComponentRegistration
<S
> ServiceOverrides(params ServiceOverride
[] overrides
)
209 return AddDescriptor(new ServiceOverrideDescriptor
<S
>(overrides
));
213 /// With the service overrides.
215 /// <param name="overrides">The overrides.</param>
216 /// <returns></returns>
217 public ComponentRegistration
<S
> ServiceOverrides(IDictionary overrides
)
219 return AddDescriptor(new ServiceOverrideDescriptor
<S
>(overrides
));
223 /// With the service overrides.
225 /// <param name="anonymous">The overrides.</param>
226 /// <returns></returns>
227 public ComponentRegistration
<S
> ServiceOverrides(object anonymous
)
229 return AddDescriptor(new ServiceOverrideDescriptor
<S
>(anonymous
));
233 /// With the configuration parameters.
235 /// <param name="parameters">The parameters.</param>
236 /// <returns></returns>
237 public ComponentRegistration
<S
> Parameters(params Parameter
[] parameters
)
239 return AddDescriptor(new ParametersDescriptor
<S
>(parameters
));
243 /// With the interceptors.
245 /// <param name="interceptors">The interceptors.</param>
246 /// <returns></returns>
247 public Interceptor
.InterceptorGroup
<S
> Interceptors(
248 params InterceptorReference
[] interceptors
)
250 return new Interceptor
.InterceptorGroup
<S
>(this, interceptors
);
254 /// Ases the startable.
256 /// <returns></returns>
257 public ComponentRegistration
<S
> Startable()
259 return AddDescriptor(new ExtendedPropertiesDescriptor
<S
>(
260 Property
.ForKey("startable").Eq(true)));
264 /// Registers this component with the <see cref="IKernel"/>.
266 /// <param name="kernel">The kernel.</param>
267 void IRegistration
.Register(IKernel kernel
)
269 if (componentModel
== null)
271 InitializeDefaults();
272 componentModel
= BuildComponentModel(kernel
);
273 kernel
.AddCustomComponent(componentModel
);
278 /// Builds the component model.
280 /// <returns></returns>
281 private ComponentModel
BuildComponentModel(IKernel kernel
)
283 IConfiguration configuration
= EnsureComponentConfiguration(kernel
);
284 foreach(ComponentDescriptor
<S
> descriptor
in descriptors
)
286 descriptor
.ApplyToConfiguration(kernel
, configuration
);
289 ComponentModel model
= kernel
.ComponentModelBuilder
.BuildModel(
290 name
, serviceType
, implementation
, null);
291 foreach(ComponentDescriptor
<S
> descriptor
in descriptors
)
293 descriptor
.ApplyToModel(kernel
, model
);
300 /// Adds the attribute descriptor.
302 /// <param name="key">The key.</param>
303 /// <param name="value">The value.</param>
304 /// <returns></returns>
305 public ComponentRegistration
<S
> AddAttributeDescriptor(string key
, string value)
307 AddDescriptor(new AttributeDescriptor
<S
>(key
, value));
312 /// Adds the descriptor.
314 /// <param name="descriptor">The descriptor.</param>
315 /// <returns></returns>
316 public ComponentRegistration
<S
> AddDescriptor(ComponentDescriptor
<S
> descriptor
)
318 descriptor
.Registration
= this;
319 descriptors
.Add(descriptor
);
323 internal void AddParameter(IKernel kernel
, ComponentModel model
, String key
, String
value)
325 IConfiguration parameters
= EnsureParametersConfiguration(kernel
);
326 MutableConfiguration parameter
= new MutableConfiguration(key
, value);
327 parameters
.Children
.Add(parameter
);
328 model
.Parameters
.Add(key
, value);
331 internal void AddParameter(IKernel kernel
, ComponentModel model
, String key
, IConfiguration
value)
333 IConfiguration parameters
= EnsureParametersConfiguration(kernel
);
334 MutableConfiguration parameter
= new MutableConfiguration(key
);
335 parameter
.Children
.Add(value);
336 parameters
.Children
.Add(parameter
);
337 model
.Parameters
.Add(key
, value);
340 private void InitializeDefaults()
342 if (implementation
== null)
344 implementation
= serviceType
;
347 if (String
.IsNullOrEmpty(name
))
349 name
= implementation
.FullName
;
353 private IConfiguration
EnsureParametersConfiguration(IKernel kernel
)
355 IConfiguration configuration
= EnsureComponentConfiguration(kernel
);
356 IConfiguration parameters
= configuration
.Children
["parameters"];
357 if (parameters
== null)
359 parameters
= new MutableConfiguration("component");
360 configuration
.Children
.Add(parameters
);
365 private IConfiguration
EnsureComponentConfiguration(IKernel kernel
)
367 IConfiguration configuration
= kernel
.ConfigurationStore
.GetComponentConfiguration(name
);
368 if (configuration
== null)
370 configuration
= new MutableConfiguration("component");
371 kernel
.ConfigurationStore
.AddComponentConfiguration(name
, configuration
);
373 return configuration
;
377 public class ComponentRegistration
: ComponentRegistration
<object>
379 public ComponentRegistration()
383 public ComponentRegistration(Type serviceType
)
385 ServiceType
= serviceType
;
388 public ComponentRegistration
For(Type serviceType
)
390 if (ServiceType
!= null)
392 String message
= String
.Format("This component has " +
393 "already been assigned service type {0}", ServiceType
.FullName
);
394 throw new ComponentRegistrationException(message
);
397 ServiceType
= serviceType
;