Reference:DependsOn
Contents |
Overview
The DependsOn attribute indicates that execution of the marked controller will be affected by the value provided in the marked field.
Usage
This attribute is specified for Property and Field level usage. Controller execution order, while being significantly influenced by the rules defined through Bind attributes, are governed by the set of Provides/Requires/DependsOn attributes. Controllers which provide a value v will run before controllers that Require or DependOn v. The order enforced here supersedes the order enforced through the Bind attribute.
- Be Careful
- If the required value is not supplied by any controller in the execution path, the controller will be invoked with
nullsupplied to the marked field. While if we use Requires attribute, we'll get a run-time error.
Name
The name property can be used to redefine the resource name made public by this attribute. The value defaults to the name of the property or field marked.
Example
The following code demonstrates how to use the DependsOn attribute to enforce an execution order. By combining the Request and DependsOn attributes, the Home controller notifies the system that if there is a provider of a preferenceReset value, it should run first. In this example, if a request comes in for /default/clear-prefs, the ClearPreferences controller will execute before the Home controller, providing it with true for preferenceReset
[Bind("/default/clear-prefs")] public class ClearPreferences : AbstractController { [Request] protected bool preferenceReset; public override void DoProcessRequest(IExecutionContext context) { ContentTypeHelper.Instance.SetAsDefault(null); preferenceReset = true; } } [Bind("/")] [Bind("/default")] [RenderWith("Templates/home.django")] public class Home : AbstractController { [Request, DependsOn] protected bool preferenceReset; public override void DoProcessRequest(IExecutionContext context) { ContentType def = ContentTypeHelper.Instance.GetLastDefault(); if (def == null || preferenceReset) return; context.Transfer("/action/search/" + (def.Equals(ContentType.Resume) ? "resume" : "ad")); } }