Dynamic Actions

Dynamic Actions MonoRail considers every public instance method of a controller an action, for certain situations this is not enough. For example, you want to decide on the name for a section in the URL at runtime when the data is available; or a set of controllers that inherit from a distinct common super class need the same action which can be defined in the super class. Dynamic Actions are a way to associate code with a name in runtime. Using it A dynamic action is nothing more than an implementation of the interface IDynamicAction: public interface IDynamicAction { /// /// Implementors should perform the action /// upon this invocation /// void Execute(Controller controller); }You can associate a dynamic action with a controller using the DynamicActions property: public class MyController : Controller { public MyController { DynamicActions[“index”] = new IndexDynamicAction(); } }Dynamic Action Providers Dynamic Action Providers are in charge of adding dynamic actions to controllers. They can be associated with controllers using the DynamicActionAttribute: [DynamicAction(typeof(MyDynActionProvider))] public class MyController : Controller { public MyController { } }The implementation of the provider can be something static (ie. always add the same set of actions) or can “read” something from the controller or the logged user; and add the actions accordingly: public class MyDynActionProvider : IDynamicActionProvider { public void IncludeActions(Controller controller) { controller.DynamicActions[“index”] = new IndexDynamicAction(); } }

About Nguyễn Viết Hiền

Passionate, Loyal
This entry was posted in ASP.NET MVC, C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s