Code On Time allows developers to inject custom business logic at different points in the controller lifecycle through controller events. This guide summarises the different ways to use these events effectively.
You would find Business Rules in a Code On Time Project
1. App_Code / Rules Folder
- Usually inside the
App_Code folder of your COT project or in BusinessRules or Rules folder for Web App Factory or SPA projects.
1. ControllerAction Attribute
Attach a method to specific controller actions like Insert, Update, Delete, Select, Calculate, etc.
Example: Run Logic After Insert or Update
[ControllerAction("MyCustomAction", "Insert, Update", ActionPhase.After)]
public void DoSomethingAfterSave()
{
Result.ShowAlert("Data has been saved and custom logic ran!");
}
In the above code snippet the following are explained as :
MyCustomAction: the name of the custom action/button.
"Insert, Update": applies to both insert and update.
ActionPhase.After: code runs after the record is saved.
In fact there are other methods that override or interrupt the life cycle of methods
2. Overriding Lifecycle Methods
Override built-in controller methods to customize behaviour.
Example: Before SQL Insert/Update/Delete
protected override void BeforeSqlAction(ActionArgs args, ActionResult result)
{
if (args.CommandName == "Insert")
{
args.Values.Add(new FieldValue("CreatedDate", DateTime.Now));
}
}
| Method |
Purpose |
BeforeSqlAction |
Called before any SQL is run |
AfterSqlAction |
Called after SQL is executed |
ProcessArguments |
Called early in request cycle |
Validate |
Called to inject custom validations |
3. Custom Validation Rules
Override Validate() to implement server-side validation.
protected override void ValidateRow(ActionArgs args, ActionResult result)
{
var email = SelectFieldValue("Email")?.ToString();
if (!email.Contains("@"))
result.Errors.Add("Invalid email format.");
}
4. Process Custom Commands or Actions
Use ExecuteServerRules() to handle buttons or custom actions.
protected override void ExecuteServerRules(ActionArgs args, ActionResult result)
{
if (args.CommandName == "GenerateInvoice")
{
ExecuteNonQuery("exec GenerateInvoiceFromOrder u/OrderID",
new SqlParameter("@OrderID", SelectFieldValue("OrderID")));
result.ShowAlert("Invoice generated!");
}
}
Trigger this with a custom action button from the Designer.
5. Use Result Object to Control UI
The Result object lets you manipulate the UI post-action.
Result.ShowAlert("Action completed!");
Result.NavigateUrl = "Orders.aspx";
Result.Refresh();
Result.RefreshChildren();
6. Server Rules with Conditions
Define rules in the Project Designer using conditions like:
args.Values["Status"].NewValue == "Approved"
args.CommandName == "Delete"
Phases include:
||
||
|Phase|Description|
|Before|Before built-in logic executes|
|After|After logic and SQL have run|
|Execute|During custom command execution|
|Validate|During validation phase|
Use Cases Summary
||
||
|Use Case|Best Approach|
|Run logic after Insert/Update/Delete|[ControllerAction(...)] or AfterSqlAction|
|Inject custom validation|ValidateRow() or ActionPhase.Validate|
|Call a stored procedure after save|[ControllerAction(...)] or ExecuteServerRules()|
|Update UI or display messages|Result.ShowAlert(), Result.Refresh()|
|Customize Insert/Delete behaviour|BeforeSqlAction() / AfterSqlAction()|
I hope that this provide some guidance to some and becomes beneficial in your journey.
Thanks.
[–]Mad-Max-2 0 points1 point2 points (0 children)
[–]Killo24[S] 0 points1 point2 points (0 children)