Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / DefaultControllerTree.cs
blob399979cc3e741cec22d9ea3a377c33fe66260e49
1 // Copyright 2004-2007 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.Framework.Services
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
21 /// <summary>
22 /// Default implementation of <see cref="IControllerTree"/>.
23 /// Represents an binary tree of registered controllers.
24 /// <para>
25 /// It is used by the controller factory to resolve a controller instance
26 /// based on the specified area (which is optional) and controller name
27 /// </para>
28 /// <seealso cref="IControllerTree"/>
29 /// <seealso cref="Castle.MonoRail.Framework.Services.AbstractControllerFactory"/>
30 /// </summary>
31 public sealed class DefaultControllerTree : IControllerTree
33 /// <summary>
34 /// The area the controller belongs to.
35 /// The default area is <c>String.Empty</c>
36 /// </summary>
37 private readonly String area;
38 /// <summary>
39 /// A dictionary of controllers that belongs to this node (area)
40 /// </summary>
41 private readonly IDictionary controllers;
42 /// <summary>
43 /// The controllers node on the left
44 /// </summary>
45 private DefaultControllerTree left;
46 /// <summary>
47 /// The controllers node on the right
48 /// </summary>
49 private DefaultControllerTree right;
51 /// <summary>
52 /// Occurs when a controller is added to the controller tree.
53 /// </summary>
54 public event EventHandler<ControllerAddedEventArgs> ControllerAdded;
56 /// <summary>
57 /// Constructs a <c>ControllerTree</c> with an empty area
58 /// </summary>
59 public DefaultControllerTree() : this(String.Empty)
63 /// <summary>
64 /// Constructs a <c>ControllerTree</c> specifying an area
65 /// </summary>
66 public DefaultControllerTree(String areaName)
68 if (areaName == null) throw new ArgumentNullException("areaName");
70 area = areaName;
71 controllers = new HybridDictionary(true);
74 private void OnControllerAdded(String area, String controllerName, Type controller)
76 if(ControllerAdded != null) {
77 ControllerAddedEventArgs args = new ControllerAddedEventArgs(area, controllerName, controller);
78 ControllerAdded(this, args);
82 /// <summary>
83 /// Register a controller on the tree. If the specified
84 /// area name matches the current node, the controller is
85 /// register on the node itself, otherwise on the right or
86 /// on the left node.
87 /// </summary>
88 /// <remarks>
89 /// Note that the controller is an <c>object</c>. That allows
90 /// different implementation of a controller factory to register
91 /// different representation of what a controller is (a name, a descriptor etc)
92 /// </remarks>
93 /// <param name="areaName">The area name, or <c>String.Empty</c></param>
94 /// <param name="controllerName">The controller name</param>
95 /// <param name="controller">The controller representation</param>
96 public void AddController(String areaName, String controllerName, Type controller)
98 if (areaName == null) throw new ArgumentNullException("areaName");
99 if (controllerName == null) throw new ArgumentNullException("controllerName");
100 if (controller == null) throw new ArgumentNullException("controller");
102 int cmp = String.Compare(areaName, area, true);
104 if (cmp == 0)
106 // If it's the same area, register the controller
107 controllers[controllerName] = controller;
109 else
111 // Otherwise, check if the controller should be registered
112 // on the left or on the right
114 DefaultControllerTree node;
116 if (cmp < 0)
118 if (left == null)
120 left = new DefaultControllerTree(areaName);
122 node = left;
124 else
126 if (right == null)
128 right = new DefaultControllerTree(areaName);
130 node = right;
133 node.AddController(areaName, controllerName, controller);
134 OnControllerAdded(areaName, controllerName, controller);
138 /// <summary>
139 /// Returns a controller previously registered.
140 /// </summary>
141 /// <param name="areaName">The area name, or <c>String.Empty</c></param>
142 /// <param name="controllerName">The controller name</param>
143 /// <returns>The controller representation or null</returns>
144 public Type GetController(String areaName, String controllerName)
146 if (areaName == null) throw new ArgumentNullException("areaName");
147 if (controllerName == null) throw new ArgumentNullException("controllerName");
149 int cmp = String.Compare(areaName, area, true);
151 if (cmp == 0)
153 return (Type) controllers[controllerName];
155 else
157 DefaultControllerTree node;
159 if (cmp < 0)
161 node = left;
163 else
165 node = right;
168 if (node != null)
170 return node.GetController(areaName, controllerName);
174 return null;
178 /// <summary>
179 /// Event class that represents details of the controller added
180 /// to the controller tree
181 /// </summary>
182 public sealed class ControllerAddedEventArgs : System.EventArgs
184 private readonly String area;
185 private readonly String controllerName;
186 private readonly Type controllerType;
188 /// <summary>
189 /// Initializes a new instance of the <see cref="ControllerAddedEventArgs"/> class.
190 /// </summary>
191 /// <param name="area">The area.</param>
192 /// <param name="controllerName">Name of the controller.</param>
193 /// <param name="controllerType">Type of the controller.</param>
194 public ControllerAddedEventArgs(string area, string controllerName, Type controllerType)
196 this.area = area;
197 this.controllerName = controllerName;
198 this.controllerType = controllerType;
201 /// <summary>
202 /// Gets the area.
203 /// </summary>
204 /// <value>The area.</value>
205 public string Area
207 get { return area; }
210 /// <summary>
211 /// Gets the name of the controller.
212 /// </summary>
213 /// <value>The name of the controller.</value>
214 public string ControllerName
216 get { return controllerName; }
219 /// <summary>
220 /// Gets the type of the controller.
221 /// </summary>
222 /// <value>The type of the controller.</value>
223 public Type ControllerType
225 get { return controllerType; }