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
> : IComponentRegistration
31 private bool overwrite
;
32 private readonly Type serviceType
;
33 private Type classType
;
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()
45 protected ComponentRegistration(Type serviceType
)
48 this.serviceType
= serviceType
;
49 descriptors
= new List
<ComponentDescriptor
<S
>>();
52 internal bool Overwrite
54 get { return overwrite; }
58 /// With the overwrite.
60 /// <returns></returns>
61 public ComponentRegistration
<S
> OverWrite()
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
);
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
);
105 /// With the instance.
107 /// <param name="instance">The instance.</param>
108 /// <returns></returns>
109 public ComponentRegistration
<S
> Instance(S instance
)
111 return AddDescriptor(new ComponentInstanceDescriptior
<S
>(instance
));
117 /// <value>The proxy.</value>
118 public Proxy
.ProxyGroup
<S
> Proxy
120 get { return new Proxy.ProxyGroup<S>(this); }
124 /// Gets the with lifestyle.
126 /// <value>The with lifestyle.</value>
127 public Lifestyle
.LifestyleGroup
<S
> LifeStyle
129 get { return new Lifestyle.LifestyleGroup<S>(this); }
133 /// With the activator.
135 /// <returns></returns>
136 public ComponentRegistration
<S
> Activator
<A
>() where A
: IComponentActivator
138 return AddAttributeDescriptor("componentActivatorType", typeof(A
).AssemblyQualifiedName
);
142 /// With the extended properties.
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
));
152 /// With the extended properties.
154 /// <param name="anonymous">The properties.</param>
155 /// <returns></returns>
156 public ComponentRegistration
<S
> ExtendedProperties(object anonymous
)
158 return AddDescriptor(new ExtendedPropertiesDescriptor
<S
>(anonymous
));
162 /// With the custom dependencies.
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
));
172 /// With the custom dependencies.
174 /// <param name="dependencies">The dependencies.</param>
175 /// <returns></returns>
176 public ComponentRegistration
<S
> CustomDependencies(IDictionary dependencies
)
178 return AddDescriptor(new CustomDependencyDescriptor
<S
>(dependencies
));
182 /// With the custom dependencies.
184 /// <param name="anonymous">The dependencies.</param>
185 /// <returns></returns>
186 public ComponentRegistration
<S
> CustomDependencies(object anonymous
)
188 return AddDescriptor(new CustomDependencyDescriptor
<S
>(anonymous
));
192 /// With the service overrides.
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
));
202 /// With the service overrides.
204 /// <param name="overrides">The overrides.</param>
205 /// <returns></returns>
206 public ComponentRegistration
<S
> ServiceOverrides(IDictionary overrides
)
208 return AddDescriptor(new ServiceOverrideDescriptor
<S
>(overrides
));
212 /// With the service overrides.
214 /// <param name="anonymous">The overrides.</param>
215 /// <returns></returns>
216 public ComponentRegistration
<S
> ServiceOverrides(object anonymous
)
218 return AddDescriptor(new ServiceOverrideDescriptor
<S
>(anonymous
));
222 /// With the interceptors.
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
);
233 /// Ases the startable.
235 /// <returns></returns>
236 public ComponentRegistration
<S
> Startable()
238 return AddDescriptor(new ExtendedPropertiesDescriptor
<S
>(
239 Property
.ForKey("startable").Eq(true)));
243 /// Registers this component with the <see cref="IKernel"/>.
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
);
257 /// Builds the component model.
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
);
279 /// Adds the attribute descriptor.
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));
291 /// Adds the descriptor.
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
);
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
)