Business Logic Framework - How To:
Assuming that you would like to display a list of customers and you would like to create a new column that shows the complete name of the customer. This can be done in two separate ways depending on your requirements. The calculated column can be created at database level or it can be created in the result set returned by the database. The following code snippet shows you how to add a calculated column at the database level:
| using (Customers customer = new Customers()) | |
| { | |
| // Create a new Field of Type StringField | |
| // Passing the customer object instance to the field constructor will simplify the process and this field will automatically be included in the result set. | |
| StringField fullName = new StringField(customer); | |
| // We will mark the IsCustom property to true for this field to let the business object know that this field is a custom field and should not be treated like a regular field. | |
| fullName.IsCustom = true; | |
| // Since there is no column in the underlying table named "FullName", we want to make sure that we don't include this field in Insert or Updates | |
| // The following line shows how to exclude this field from Inserts or Updates. | |
| fullName.AllowSave = false; | |
| // We must assign an alias (ResultSetName) to the fullName field object. The column in the result set will be named with this value. | |
| // NOTE: We recommend that you use the GetResultSetName() method of the field when trying to access the column from the result set as this will automatically make sure you get the correct column name. | |
| fullName.ResultSetName = "FullName"; | |
| // In a Custom field the Name property is used to specify any calculation expression possible. | |
| // Here we specify that first the LastName column value should appear then it should be appended by | |
| // a comma and a space. Lastly the FirstName value will be appended. | |
| fullName.Name = customer.LastName.Name.Value + " + ', ' + " + customer.FirstName.Name.Value; | |
| this.GridView.DataSource = customer.Find(); | |
| this.GridView.DataMember = customer.GetResultSetName(); | |
| } | |
| Using customer As New Customers() | |
| ' Create a new Field of Type StringField | |
| ' Passing the customer object instance to the field constructor will simplify the process and this field will automatically be included in the result set. | |
| Dim fullName As New StringField(customer) | |
| ' We will mark the IsCustom property to true for this field to let the business object know that this field is a custom field and should not be treated like a regular field. | |
| fullName.IsCustom = True | |
| ' Since there is no column in the underlying table named "FullName", we want to make sure that we don't include this field in Insert or Updates | |
| ' The following line shows how to exclude this field from Inserts or Updates. | |
| fullName.AllowSave = False | |
| ' We must assign an alias (ResultSetName) to the fullName field object. The column in the result set will be named with this value. | |
| ' NOTE: We recommend that you use the GetResultSetName() method of the field when trying to access the column from the result set as this will automatically make sure you get the correct column name. | |
| fullName.ResultSetName = "FullName" | |
| ' In a Custom field the Name property is used to specify any calculation expression possible. | |
| ' Here we specify that first the LastName column value should appear then it should be appended by | |
| ' a comma and a space. Lastly the FirstName value will be appended. | |
| fullName.Name = customer.LastName.Name.Value + " + ', ' + " + customer.FirstName.Name.Value | |
| Me.GridView.DataSource = customer.Find() | |
| Me.GridView.DataMember = customer.GetResultSetName() | |
| End Using |
The other alternative is to add a calculated column to the DataSet returned by Find method. Here is an example:
| using (Customers customer = new Customers()) | |
| { | |
| // Run the Find method, which will return all rows available in Customers table since there is no search criteria specified. | |
| customer.Find(); | |
| // Now check and make sure that the Find was successful and there is a table containing the customer data in the DataSet returned by Find. | |
| if (customer.ResultSet.Tables.Contains(customer.GetResultSetName())) | |
| { | |
| // We can use the DataSet's calculated column capability to add a calculated column. | |
| DataColumn dc = new DataColumn(); | |
| dc.ColumnName = "FullName"; | |
| dc.Expression = "[LastName] + ', ' + [FirstName]"; | |
| // the following line will add this calculated column to the table containing the customer data. | |
| customer.ResultSet.Tables[customer.GetResultSetName()].Columns.Add(dc); | |
| // We can access the DataSet returned by the Find method in the "ResultSet" property of the business object. | |
| // NOTE: By default the "AddResultsToDataSet" is set to true and hence the data will be available in the ResultSet property. However, if you set the | |
| // "AddResultsToDataSet" to false, then the DataSet will only be returned by the Find method and will not be stored in the "ResultSet" property. | |
| this.GridView.DataSource = customer.ResultSet; | |
| this.GridView.DataMember = customer.GetResultSetName(); | |
| } | |
| } | |
| Using customer As New Customers() | |
| ' Run the Find method, which will return all rows available in Customers table since there is no search criteria specified. | |
| customer.Find() | |
| ' Now check and make sure that the Find was successful and there is a table containing the customer data in the DataSet returned by Find. | |
| If customer.ResultSet.Tables.Contains(customer.GetResultSetName()) Then | |
| ' We can use the DataSet's calculated column capability to add a calculated column. | |
| Dim dc As New DataColumn() | |
| dc.ColumnName = "FullName" | |
| dc.Expression = "[LastName] + ', ' + [FirstName]" | |
| ' the following line will add this calculated column to the table containing the customer data. | |
| customer.ResultSet.Tables(customer.GetResultSetName()).Columns.Add(dc) | |
| ' We can access the DataSet returned by the Find method in the "ResultSet" property of the business object. | |
| ' NOTE: By default the "AddResultsToDataSet" is set to true and hence the data will be available in the ResultSet property. However, if you set the | |
| ' "AddResultsToDataSet" to false, then the DataSet will only be returned by the Find method and will not be stored in the "ResultSet" property. | |
| Me.GridView.DataSource = customer.ResultSet | |
| Me.GridView.DataMember = customer.GetResultSetName() | |
| End If | |
| End Using |
Comparison of the two methods:
| Database Level | DataSet Level |
| Performed at the SQL Level by the Database | Performed by the DataSet in the memory |
| The expression code you write is database dependent. Hence if you choose to move your application to a different database server you will need to make modifications to your custom column for it to work correctly. | The expression code is NOT database dependent. Hence the code you write will work irrespective of the underlying database type. |
| Unable to calculate values while in disconnected state and hence the values only are updated after insert/update is completed to the database and database is queried for the same data again. | Values are automatically updated in the disconnected state. There is no need to go back and query the database again to update calculated columns if any of the underlying values have changed. |
This above comparison is meant to be brief highlight of each method and is no way meant as a comprehensive analysis of the performance of each of these two techniques.
