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
.Framework
.Services
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
22 /// Default implementation of <see cref="IControllerTree"/>.
23 /// Represents an binary tree of registered controllers.
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
28 /// <seealso cref="IControllerTree"/>
29 /// <seealso cref="Castle.MonoRail.Framework.Services.AbstractControllerFactory"/>
31 public sealed class DefaultControllerTree
: IControllerTree
34 /// The area the controller belongs to.
35 /// The default area is <c>String.Empty</c>
37 private readonly String area
;
40 /// A dictionary of controllers that belongs to this node (area)
42 private readonly IDictionary controllers
;
45 /// The controllers node on the left
47 private DefaultControllerTree left
;
50 /// The controllers node on the right
52 private DefaultControllerTree right
;
55 /// Occurs when a controller is added to the controller tree.
57 public event EventHandler
<ControllerAddedEventArgs
> ControllerAdded
;
60 /// Constructs a <c>ControllerTree</c> with an empty area
62 public DefaultControllerTree() : this(String
.Empty
)
67 /// Constructs a <c>ControllerTree</c> specifying an area
69 public DefaultControllerTree(String areaName
)
71 if (areaName
== null) throw new ArgumentNullException("areaName");
74 controllers
= new HybridDictionary(true);
77 private void OnControllerAdded(String area
, String controllerName
, Type controller
)
79 if (ControllerAdded
!= null)
81 ControllerAddedEventArgs args
= new ControllerAddedEventArgs(area
, controllerName
, controller
);
82 ControllerAdded(this, args
);
87 /// Register a controller on the tree. If the specified
88 /// area name matches the current node, the controller is
89 /// register on the node itself, otherwise on the right or
93 /// Note that the controller is an <c>object</c>. That allows
94 /// different implementation of a controller factory to register
95 /// different representation of what a controller is (a name, a descriptor etc)
97 /// <param name="areaName">The area name, or <c>String.Empty</c></param>
98 /// <param name="controllerName">The controller name</param>
99 /// <param name="controller">The controller representation</param>
100 public void AddController(String areaName
, String controllerName
, Type controller
)
102 if (areaName
== null) throw new ArgumentNullException("areaName");
103 if (controllerName
== null) throw new ArgumentNullException("controllerName");
104 if (controller
== null) throw new ArgumentNullException("controller");
106 int cmp
= String
.Compare(areaName
, area
, true);
110 // If it's the same area, register the controller
111 controllers
[controllerName
] = controller
;
115 // Otherwise, check if the controller should be registered
116 // on the left or on the right
118 DefaultControllerTree node
;
124 left
= new DefaultControllerTree(areaName
);
132 right
= new DefaultControllerTree(areaName
);
137 node
.AddController(areaName
, controllerName
, controller
);
138 OnControllerAdded(areaName
, controllerName
, controller
);
143 /// Returns a controller previously registered.
145 /// <param name="areaName">The area name, or <c>String.Empty</c></param>
146 /// <param name="controllerName">The controller name</param>
147 /// <returns>The controller representation or null</returns>
148 public Type
GetController(String areaName
, String controllerName
)
150 if (areaName
== null) throw new ArgumentNullException("areaName");
151 if (controllerName
== null) throw new ArgumentNullException("controllerName");
153 int cmp
= String
.Compare(areaName
, area
, true);
157 return (Type
) controllers
[controllerName
];
161 DefaultControllerTree node
;
174 return node
.GetController(areaName
, controllerName
);
183 /// Event class that represents details of the controller added
184 /// to the controller tree
186 public sealed class ControllerAddedEventArgs
: System
.EventArgs
188 private readonly String area
;
189 private readonly String controllerName
;
190 private readonly Type controllerType
;
193 /// Initializes a new instance of the <see cref="ControllerAddedEventArgs"/> class.
195 /// <param name="area">The area.</param>
196 /// <param name="controllerName">Name of the controller.</param>
197 /// <param name="controllerType">Type of the controller.</param>
198 public ControllerAddedEventArgs(string area
, string controllerName
, Type controllerType
)
201 this.controllerName
= controllerName
;
202 this.controllerType
= controllerType
;
208 /// <value>The area.</value>
215 /// Gets the name of the controller.
217 /// <value>The name of the controller.</value>
218 public string ControllerName
220 get { return controllerName; }
224 /// Gets the type of the controller.
226 /// <value>The type of the controller.</value>
227 public Type ControllerType
229 get { return controllerType; }