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
;
22 using Castle
.MicroKernel
.Util
;
24 public class ServiceOverrideDescriptor
<S
> : AbstractPropertyDescriptor
<S
>
26 public ServiceOverrideDescriptor(params ServiceOverride
[] overrides
)
31 public ServiceOverrideDescriptor(IDictionary dictionary
)
36 public ServiceOverrideDescriptor(object overridesAsAnonymousType
)
37 : base(new ReflectionBasedDictionaryAdapter(overridesAsAnonymousType
))
41 protected override void ApplyProperty(IKernel kernel
, ComponentModel model
,
42 String key
, Object
value, Property property
)
46 ApplySimpleReference(kernel
, model
, key
, (String
)value);
48 else if (value is IEnumerable
<String
>)
50 ServiceOverride serviceOverride
= (ServiceOverride
)property
;
51 ApplyReferenceList(kernel
, model
, key
, (IEnumerable
<String
>)value, serviceOverride
);
55 private void ApplySimpleReference(IKernel kernel
, ComponentModel model
,
56 String key
, String componentKey
)
58 String reference
= FormattedReferenceExpression(componentKey
);
59 Registration
.AddParameter(kernel
, model
, key
, reference
);
62 private void ApplyReferenceList(IKernel kernel
, ComponentModel model
,
63 String key
, IEnumerable
<String
> componentKeys
,
64 ServiceOverride serviceOverride
)
66 MutableConfiguration list
= new MutableConfiguration("list");
68 if (serviceOverride
!= null && serviceOverride
.Type
!= null)
70 list
.Attributes
.Add("type", serviceOverride
.Type
.AssemblyQualifiedName
);
73 foreach (String componentKey
in componentKeys
)
75 String reference
= FormattedReferenceExpression(componentKey
);
76 MutableConfiguration node
= new MutableConfiguration("item", reference
);
77 list
.Children
.Add(node
);
80 Registration
.AddParameter(kernel
, model
, key
, list
);
83 private static String
FormattedReferenceExpression(String
value)
85 if (!ReferenceExpressionUtil
.IsReference(value))
87 value = String
.Format("${{{0}}}", value);