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
.MonoRail
.WindsorExtension
18 using Castle
.MicroKernel
.Facilities
;
19 using Castle
.MonoRail
.Framework
;
20 using Castle
.MonoRail
.Framework
.Descriptors
;
21 using Castle
.MonoRail
.Framework
.Services
;
22 using Castle
.MonoRail
.Framework
.Services
.Utils
;
25 /// Facility responsible for registering the controllers in
26 /// the controllerTree.
28 public class MonoRailFacility
: AbstractFacility
30 private IControllerTree controllerTree
;
31 private IViewComponentRegistry componentRegistry
;
33 protected override void Init()
35 RegisterWindsorLocatorStrategyWithinMonoRail();
37 Kernel
.AddComponent("mr.controllertree", typeof(IControllerTree
), typeof(DefaultControllerTree
));
38 Kernel
.AddComponent("mr.wizardpagefactory", typeof(IWizardPageFactory
), typeof(DefaultWizardPageFactory
));
39 Kernel
.AddComponent("mr.viewcomponentregistry", typeof(IViewComponentRegistry
), typeof(DefaultViewComponentRegistry
));
40 Kernel
.AddComponent("mr.controllerfactory", typeof(IControllerFactory
), typeof(WindsorControllerFactory
));
41 Kernel
.AddComponent("mr.filterFactory", typeof(IFilterFactory
), typeof(WindsorFilterFactory
));
42 Kernel
.AddComponent("mr.viewcompfactory", typeof(IViewComponentFactory
), typeof(WindsorViewComponentFactory
));
43 Kernel
.AddComponent("mr.helperfactory", typeof(IHelperFactory
), typeof(WindsorHelperFactory
));
45 controllerTree
= Kernel
.Resolve
<IControllerTree
>();
46 componentRegistry
= Kernel
.Resolve
<IViewComponentRegistry
>();
48 Kernel
.ComponentModelCreated
+= OnComponentModelCreated
;
51 private void RegisterWindsorLocatorStrategyWithinMonoRail()
53 ServiceProviderLocator
.Instance
.AddLocatorStrategy(new WindsorAccessorStrategy());
56 private void OnComponentModelCreated(ComponentModel model
)
58 bool isController
= typeof(IController
).IsAssignableFrom(model
.Implementation
);
59 bool isViewComponent
= typeof(ViewComponent
).IsAssignableFrom(model
.Implementation
);
61 if (!isController
&& !isViewComponent
)
66 // Ensure it's transient
67 model
.LifestyleType
= LifestyleType
.Transient
;
68 model
.InspectionBehavior
= PropertiesInspectionBehavior
.DeclaredOnly
;
72 ControllerDescriptor descriptor
= ControllerInspectionUtil
.Inspect(model
.Implementation
);
74 controllerTree
.AddController(descriptor
.Area
, descriptor
.Name
, model
.Implementation
);
79 componentRegistry
.AddViewComponent(model
.Name
, model
.Implementation
);
83 public class WindsorAccessorStrategy
: ServiceProviderLocator
.IAccessorStrategy
85 public IServiceProviderEx
LocateProvider()
87 return WindsorContainerAccessorUtil
.ObtainContainer();