More passing tests.
[castle.git] / MonoRail / Castle.MonoRail.WindsorExtension / MonoRailFacility.cs
blob32110a4563e22fc8e1448e8431dfa97a6357ed46
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using Castle.Core;
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;
24 /// <summary>
25 /// Facility responsible for registering the controllers in
26 /// the controllerTree.
27 /// </summary>
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)
63 return;
66 // Ensure it's transient
67 model.LifestyleType = LifestyleType.Transient;
68 model.InspectionBehavior = PropertiesInspectionBehavior.DeclaredOnly;
70 if (isController)
72 ControllerDescriptor descriptor = ControllerInspectionUtil.Inspect(model.Implementation);
74 controllerTree.AddController(descriptor.Area, descriptor.Name, model.Implementation);
77 if (isViewComponent)
79 componentRegistry.AddViewComponent(model.Name, model.Implementation);
83 public class WindsorAccessorStrategy : ServiceProviderLocator.IAccessorStrategy
85 public IServiceProviderEx LocateProvider()
87 return WindsorContainerAccessorUtil.ObtainContainer();