The following screen shows a customer record from the Northwind sample presented in edit mode. Suppose that you want to display an action on the action bar that allows automatic calculation of certain field values.
Start the application generator and click on the project name on the start page. Select the Design option to bring up the Project Designer.
Activate the Controllers tab in Project Explorer, right-click Customers / Actions / ag4 (ActionBar) node and select New Action.
Enter the following values in the properties of the action.
Property | Value |
Command Name | Custom |
Command Argument | CalcValues |
Header Text | Calculate Values |
When Last Command Name | Edit |
Click OK button to save the action.
Press Browse on the tool bar and start editing any customer record. You will notice the Calculate Values option on the action bar. The new action has been added to the “flat” action group ag4, which makes the action render directly on the action bar instead of being displayed as a child option of the parent action group menu item.
Clicking on Calculate Values option will close the edit form and the grid view of customers will be displayed.
You can handle a custom action in a custom business rules or shared business rules class.
Here is an example of a shared business rules class that changes Company Name and Contact Name fields.
C#
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using MyCompany.Data;
namespace MyCompany.Rules
{
public partial class SharedBusinessRules : MyCompany.Data.BusinessRules
{
[ControllerAction("Customers", "Custom", "CalcValues")]
public void ChangeCompanyAndContactNames(string companyName, string contactName)
{
UpdateFieldValue("CompanyName", companyName + "+");
UpdateFieldValue("ContactName", contactName + "-");
}
}
}
Visual Basic:
Imports MyCompany.Data
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Linq
Namespace MyCompany.Rules
Partial Public Class SharedBusinessRules
Inherits MyCompany.Data.BusinessRules
<ControllerAction("Customers", "Custom", "CalcValues")>
Public Sub ChangeCompanyAndContactNames(ByVal companyName As String,
ByVal contactName As String)
UpdateFieldValue("CompanyName", companyName + "+")
UpdateFieldValue("ContactName", contactName + "-")
End Sub
End Class
End Namespace
The implementation is invoking UpdateFieldValue method. The first argument of the method indicates the name of the data field. The seconds specifies the new value. Action “Custom” does not update the database. Any data fields changed during its execution will be packages and retuned to the client web browser. The client library of the application will replace the old values with the new ones.
Save the file ~/Rules/SharedBusinessRules.cs(vb), navigate to your application and start editing any customer record. Click Calculate Values and you will see that the characters “+” and “-” are added to the Customer Name and Contact Name fields. The record will remain in edit mode.
Business rules have complete access to the entire set of Microsoft.NET APIs.
A more complex version of the business rules class processing the same Custom action is presented next.
C#:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using MyCompany.Data;
namespace MyCompany.Rules
{
public partial class SharedBusinessRules : MyCompany.Data.BusinessRules
{
[ControllerAction("Customers", "Custom", "CalcValues")]
public void ChangeCompanyAndContactNames(string companyName,
string city, string country, string customerID)
{
UpdateFieldValue("City", country);
UpdateFieldValue("Country", city);
string newCompanyName = companyName + ": " + SqlText.ExecuteScalar(
"select City + '/' + Country from Customers " +
"where CustomerID = @CustomerID", customerID);
UpdateFieldValue("CompanyName", newCompanyName);
}
}
}
Visual Basic:
Imports MyCompany.Data
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Linq
Namespace MyCompany.Rules
Partial Public Class SharedBusinessRules
Inherits MyCompany.Data.BusinessRules
<ControllerAction("Customers", "Custom", "CalcValues")>
Public Sub ChangeCompanyAndContactNames(ByVal companyName As String,
ByVal city As String, ByVal country As String, ByVal customerID As String)
UpdateFieldValue("City", country)
UpdateFieldValue("Country", city)
Dim newCompanyName As String = companyName + ": " + SqlText.ExecuteScalar(
"select City + '/' + Country from Customers " +
"where CustomerID = @CustomerID", customerID)
UpdateFieldValue("CompanyName", newCompanyName)
End Sub
End Class
End Namespace
This is the result of action execution if you select the customer Around the Horn, start editing the record, and click Calculate Values option on the action bar.
- Client-side values of fields City and Region are swapped and sent back to the client
- The client-side value of the field Company Name is appended with the database values of fields City and Country.
Similar calculations can be performed without the business rules class. If you are more comfortable with SQL language and consider yourself to be a database person then learn about implementing calculations with “SQL” action.