1 // Copyright 2004-2007 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
;
19 using Castle
.MicroKernel
.Facilities
;
20 using Castle
.MonoRail
.Framework
;
21 using Castle
.MonoRail
.Framework
.Internal
;
22 using Castle
.MonoRail
.Framework
.Controllers
;
23 using Castle
.MonoRail
.Framework
.Services
;
24 using Castle
.MonoRail
.Framework
.Services
.Utils
;
27 /// Facility responsible for registering the controllers in
28 /// the controllerTree.
30 public class RailsFacility
: AbstractFacility
32 private IControllerTree controllerTree
;
33 private IViewComponentRegistry componentRegistry
;
35 public RailsFacility()
39 protected override void Init()
41 RegisterWindsorLocationWithinMonoRail();
43 Kernel
.AddComponent("rails.controllertree", typeof(IControllerTree
), typeof(DefaultControllerTree
));
44 Kernel
.AddComponent("rails.wizardpagefactory", typeof(IWizardPageFactory
), typeof(DefaultWizardPageFactory
));
45 Kernel
.AddComponent("rails.viewcomponentregistry", typeof(IViewComponentRegistry
), typeof(DefaultViewComponentRegistry
));
47 controllerTree
= (IControllerTree
) Kernel
["rails.controllertree"];
48 componentRegistry
= (IViewComponentRegistry
) Kernel
["rails.viewcomponentregistry"];
50 Kernel
.ComponentModelCreated
+= OnComponentModelCreated
;
52 AddBuiltInControllers();
55 private void RegisterWindsorLocationWithinMonoRail()
57 ServiceProviderLocator
.Instance
.AddLocatorStrategy(new WindsorAccessorStrategy());
60 protected virtual void AddBuiltInControllers()
62 Kernel
.AddComponent("files", typeof(FilesController
), typeof(FilesController
));
65 private void OnComponentModelCreated(ComponentModel model
)
67 bool isController
= typeof(Controller
).IsAssignableFrom(model
.Implementation
);
68 bool isViewComponent
= typeof(ViewComponent
).IsAssignableFrom(model
.Implementation
);
70 if (!isController
&& !isViewComponent
)
75 // Ensure it's transient
76 model
.LifestyleType
= LifestyleType
.Transient
;
77 model
.InspectionBehavior
= PropertiesInspectionBehavior
.DeclaredOnly
;
81 ControllerDescriptor descriptor
= ControllerInspectionUtil
.Inspect(model
.Implementation
);
83 controllerTree
.AddController(descriptor
.Area
, descriptor
.Name
, model
.Implementation
);
88 componentRegistry
.AddViewComponent(model
.Name
, model
.Implementation
);
92 public class WindsorAccessorStrategy
: ServiceProviderLocator
.IAccessorStrategy
94 public IServiceProviderEx
LocateProvider()
96 return WindsorContainerAccessorUtil
.ObtainContainer();