| How to create an action |
An IAction is a class whose role is to save data sent by the client into a data storage system (database, LDAP directory, file system, a remote web service, etc.)
Create a class implementing IAction.
Create an IActionFactory
This class must inherit from System.Configuration.ConfigurationSection so that it can be declared in the configuration file, and implement the IActionFactory interface by returning an instance of the class created at the first step.
If the action class is thread independent, you can use the singleton pattern and have only one instance of the action class.
For more information about creating a custom ConfigurationSection, you can read this MSDN how-to: creating a custom ConfigurationSection
The Config object passed as parameter contains the request's parameters, an access to the database and the request context.
It is up to you to determine if your action has to do something; we use the ENREGISTREMENTID parameter to determine the action that needs to be performed, for example in the StandardRecordAction. We advise to use a single parameter name for this task, and to use ENREGISTREMENTID if you use our built-in actions, but this is not mandatory.
You must return true if, and only if, you have acted. This is used to determine later in the process if the RedirectAfterPostAction must redirect the client to avoid resubmission.
Actions are executed in the sequence defined in the handler and all actions are executed even if one of previous actions has declared it has acted. Thus, you can have several actions acting during the same request. |
Declare a section in the configSections part of the configuration file with your class name and assembly
<configuration> <configSections> <section name="myaction" type="MyNamespace.MyActionFactory,MyAssembly"/> </configSection> </configuration>
Use this section name in your handlers actions list
<activsoft.engine> <handlers> <handler name="myhandler" path="myhandler.ashx" defaultTemplate="1"> ... <actions> <action section="myaction"/> </actions> ... </handler> </handlers> <loader section=""/> </activsoft.engine>
This is a simple Hello World action writing to a file (which is a terrible idea if the action is used in a web application, by the way).
using Activsoft.Engine; using System.IO; namespace MyNamespace { public class MyAction : IAction { public MyAction() { } public bool Act(Config monConfig) { // Acts only if this is action number 26 if(monConfig["ENREGISTREMENTID"]=="26") { // Config.GetPath will create a file name relative to the application root path using(StreamWriter sw = new StreamWriter(Config.GetPath("hello.txt"))) { sw.WriteLine("Sent by client: {0}",monConfig["ELEMENTID"]); } return true; } return false; } } }
using Activsoft.Engine; using System.Configuration; namespace MyNamespace { public class MyActionFactory : ConfigurationSection, IActionFactory { public IAction CreateAction(Config monConfig) { return new MyAction(); } } }
<configuration> <configSections> <section name="myaction" type="MyNamespace.MyActionFactory,MyAssembly"/> <section name="activsoft.engine" type="Activsoft.Engine.EngineSection,Activsoft.Engine"/> <section name="secureFactory" type="Activsoft.Engine.Web.SecureDatabaseCookieSessionManagerFactory,Activsoft.Engine.Web"/> <section name="xslfilter" type="Activsoft.Engine.XslFilter.XslFilterSection,Activsoft.Engine"/> <section name="dashboardRenderer" type="Activsoft.Dashboard.DashboardRendererFactory,Activsoft.Dashboard"/> <section name="webLoader" type="Activsoft.Engine.Web.WebConfigLoaderConfigurationSection,Activsoft.Engine.Web"/> </configSection> <myaction/> <secureFactory cookieName="mycookie" /> <dashboardRenderer/> <activsoft.engine> <handlers> <handler name="myhandler" path="myhandler.ashx" defaultTemplate="1"> <actions> <action section="myaction"/> </actions> <extractors> </extractors> <renderers> <renderer section="dashboardRenderer"/> </renderers> <enginemodule> <session section="secureFactory"/> <params cookie="mycookie" xslpath="..\xsl"/> <database section="db"/> </enginemodule> <transformgenerator section="xslfilter"/> </handler> </handlers> <loader section="webLoader"/> </activsoft.engine> <xslfilter> <filters> <add type="Activsoft.Dashboard.XslFilter,Activsoft.Dashboard"> <javascript outputDir="..\xsljs"/> </add> </filters> </xslfilter> </configuration>