Applying patch from Jonathon Rossi
[castle.git] / Experiments / MVC / Castle.MVC / MVCFacility.cs
blob20838354752110eec166f740cd18011c4936edbc
1 #region Apache Notice
2 /*****************************************************************************
3 *
4 * Castle.MVC
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ********************************************************************************/
19 #endregion
21 using System;
22 using System.Configuration;
23 using System.Reflection;
24 using Castle.Facilities.TypedFactory;
25 using Castle.MicroKernel;
26 using Castle.MicroKernel.Facilities;
27 using Castle.Core;
28 using Castle.Core.Configuration;
29 using Castle.MVC.Controllers;
30 using Castle.MVC.LifestyleManager;
31 using Castle.MVC.States;
32 using Castle.MVC.Views;
34 namespace Castle.MVC
36 /// <summary>
37 /// Facility responsible for initialize the MVC framework.
38 /// (Registering the controllers used by the views...)
39 /// </summary>
40 public class MVCFacility : AbstractFacility
42 /// <summary>
43 /// Binding token
44 /// </summary>
45 private BindingFlags BINDING_FLAGS_SET
46 = BindingFlags.Public
47 | BindingFlags.SetProperty
48 | BindingFlags.Instance
49 | BindingFlags.SetField
52 private Assembly _assembly = null;
54 /// <summary>
55 /// Cosntructor
56 /// </summary>
57 public MVCFacility()
62 #region IFacility Members
64 /// <summary>
65 ///
66 /// </summary>
67 protected override void Init()
69 if (FacilityConfig == null)
71 throw new ConfigurationException(
72 "The MVCFacility requires an 'assembyView' child tag.");
75 IConfiguration factoriesConfig = FacilityConfig.Children["assembyView"];
76 if (factoriesConfig!=null &&
77 factoriesConfig.Value!= null && factoriesConfig.Value!= string.Empty )
79 _assembly = Assembly.Load(factoriesConfig.Value) ;
82 // Added TypedFactory to have a IState factory
83 TypedFactoryFacility facility = new TypedFactoryFacility();
85 Kernel.AddFacility("typedfactory", facility );
86 facility.AddTypedFactoryEntry( new FactoryEntry("stateFactory", typeof(IStateFactory), "Create", "Release") );
88 // Added a ControlerTree component to track controller by view
89 Kernel.AddComponent( "mvc.controllerTree", typeof(ControllerTree) );
90 Kernel.ComponentModelCreated += new ComponentModelDelegate(OnComponentModelCreated);
92 Initialize();
95 #endregion
97 private void Initialize()
99 ControllerTree tree = (ControllerTree) Kernel["mvc.controllerTree"];
101 if (_assembly != null)
103 Type[] types = _assembly.GetExportedTypes();
105 foreach( Type type in types )
107 // Web page / UserControl
108 if ( type.IsSubclassOf(typeof(WebFormView)) || type.IsSubclassOf(typeof(WebUserControlView)) )
110 // Retrieve the properties controllers
111 PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);
112 for (int i = 0; i < properties.Length; i++)
114 if (properties[i].PropertyType.IsSubclassOf(typeof(Controller)))
116 tree.AddController(type, properties[i], properties[i].PropertyType);
124 private void OnComponentModelCreated(ComponentModel model)
126 if ( !typeof(Controller).IsAssignableFrom(model.Implementation) )
128 return;
131 // Ensure its CustomLifestyle
132 model.LifestyleType = LifestyleType.Custom;
133 model.CustomLifestyle = typeof(PerRequestLifestyleManager);