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
.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
;
39 /// A dictionary of controllers that belongs to this node (area)
41 private readonly IDictionary controllers
;
43 /// The controllers node on the left
45 private DefaultControllerTree left
;
47 /// The controllers node on the right
49 private DefaultControllerTree right
;
52 /// Occurs when a controller is added to the controller tree.
54 public event EventHandler
<ControllerAddedEventArgs
> ControllerAdded
;
57 /// Constructs a <c>ControllerTree</c> with an empty area
59 public DefaultControllerTree() : this(String
.Empty
)
64 /// Constructs a <c>ControllerTree</c> specifying an area
66 public DefaultControllerTree(String areaName
)
68 if (areaName
== null) throw new ArgumentNullException("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
);
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
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)
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);
106 // If it's the same area, register the controller
107 controllers
[controllerName
] = controller
;
111 // Otherwise, check if the controller should be registered
112 // on the left or on the right
114 DefaultControllerTree node
;
120 left
= new DefaultControllerTree(areaName
);
128 right
= new DefaultControllerTree(areaName
);
133 node
.AddController(areaName
, controllerName
, controller
);
134 OnControllerAdded(areaName
, controllerName
, controller
);
139 /// Returns a controller previously registered.
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);
153 return (Type
) controllers
[controllerName
];
157 DefaultControllerTree node
;
170 return node
.GetController(areaName
, controllerName
);
179 /// Event class that represents details of the controller added
180 /// to the controller tree
182 public sealed class ControllerAddedEventArgs
: System
.EventArgs
184 private readonly String area
;
185 private readonly String controllerName
;
186 private readonly Type controllerType
;
189 /// Initializes a new instance of the <see cref="ControllerAddedEventArgs"/> class.
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
)
197 this.controllerName
= controllerName
;
198 this.controllerType
= controllerType
;
204 /// <value>The area.</value>
211 /// Gets the name of the controller.
213 /// <value>The name of the controller.</value>
214 public string ControllerName
216 get { return controllerName; }
220 /// Gets the type of the controller.
222 /// <value>The type of the controller.</value>
223 public Type ControllerType
225 get { return controllerType; }