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
18 using System
.Reflection
;
21 /// Manages the execution of the steps associated with
22 /// a controller (filters/action/disposal).
25 /// This class is statefull.
27 public class ControllerExecutor
29 private readonly object controller
;
30 private readonly IExecutionContext executionContext
;
33 /// Initializes a new instance of the <see cref="ControllerExecutor"/> class.
35 /// <param name="controller">The controller.</param>
36 /// <param name="executionContext">The execution context.</param>
37 public ControllerExecutor(object controller
, IExecutionContext executionContext
)
39 if (controller
== null) throw new ArgumentNullException("controller");
40 if (executionContext
== null) throw new ArgumentNullException("executionContext");
42 UrlInfo url
= executionContext
.OriginalUrl
;
44 IController properController
= controller
as IController
;
46 if (properController
!= null)
48 properController
.SetInitialState(url
.Area
, url
.Controller
, url
.Action
);
51 this.controller
= controller
;
52 this.executionContext
= executionContext
;
55 public ActionExecutor
SelectAction()
57 return SelectAction(executionContext
.OriginalUrl
.Action
);
60 public ActionExecutor
SelectAction(string actionName
)
62 // There is room for a clever cache here
64 // Right now let's support method action only
66 BindingFlags flags
= BindingFlags
.IgnoreCase
| BindingFlags
.Public
| BindingFlags
.Instance
;
68 MethodInfo actionMethod
= controller
.GetType().GetMethod(actionName
, flags
);
70 if (actionMethod
!= null)
72 return new MethodActionExecutor(actionMethod
);
78 public void Execute(ActionExecutor executor
)
80 if (executor
== null) throw new ArgumentNullException("executor");
82 executor
.Execute(controller
);