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
.Facilities
.EnterpriseLibrary
.Configuration
21 using Castle
.MicroKernel
;
22 using Castle
.MicroKernel
.Facilities
;
23 using Castle
.MicroKernel
.ModelBuilder
;
24 using Castle
.MicroKernel
.ComponentActivator
;
26 using Microsoft
.Practices
.EnterpriseLibrary
.Configuration
;
29 public class EnterpriseConfigurationFacility
: AbstractFacility
31 protected override void Init()
33 Kernel
.ComponentModelBuilder
.AddContributor( new EntLibConfigurationInspector() );
37 internal class EntLibConfigurationInspector
: IContributeComponentModelConstruction
39 public void ProcessModel(IKernel kernel
, ComponentModel model
)
41 if (model
.Configuration
== null) return;
43 String configKey
= model
.Configuration
.Attributes
["configurationkey"];
45 if (configKey
== null) return;
47 model
.ExtendedProperties
["configurationkey"] = configKey
;
48 model
.CustomComponentActivator
= typeof(EntLibComponentActivator
);
52 internal class EntLibComponentActivator
: AbstractComponentActivator
54 public EntLibComponentActivator(ComponentModel model
, IKernel kernel
,
55 ComponentInstanceDelegate onCreation
, ComponentInstanceDelegate onDestruction
) : base(model
, kernel
, onCreation
, onDestruction
)
59 protected override object InternalCreate()
61 String configKey
= (String
) Model
.ExtendedProperties
["configurationkey"];
63 return ConfigurationManager
.GetConfiguration(configKey
);
66 protected override void InternalDestroy(object instance
)
68 String configKey
= (String
) Model
.ExtendedProperties
["configurationkey"];
70 ConfigurationManager
.WriteConfiguration(configKey
, instance
);