Tutorial - Custom Business Logic

In real world web application, different business logic need to be implemented to support your distinct application needs. For example, your application may require that a specific date range is allowed for a particular date field. ASP.NET Maker allows you to write your custom codes to handle such scenarios easily.

In this tutorial we will show you how you can enhance the functionlity in your ASP.NET Maker generated web site by writing your own custom business logic codes. We will use the demo project and database for demostration. The demo database is a modified version of the Access demo database Northwind.mdb for better illustration.

ASP.NET Maker 3 uses n-tier design

ASP.NET Maker 3 generates ASP.NET web site in the following n-tier architecture.

  • Presentation Layer
    ASP.NET Maker generates list/view/search/add/edit/delete pages under the destination folder.

  • Business Logic Layer
    ASP.NET Maker generates business layer codes for each Table/Custom View under <Application root folder>/App_Code with file suffix bll.cs/vb and bll_base.cs/vb.

  • Data Access Layer
    ASP.NET Maker generates data access layer codes for each Table/Custom View under <Application root folder>/App_Code with the following file suffix:
    • Data Class: dal.cs/vb and dal_base.cs/vb
      The classes to manipulate data from the database.
    • Row Class: row.cs/vb and row_base.cs/vb
      The classes to store the values of a row in database.
    • Info Class: inf.cs/vb and inf_base.cs/vb
      The classes to store the information about the fields in table
    • Key Class: key.cs/vb and key_base.cs/vb
      The classes to store the primary key information of the table

For each class, ASP.NET Maker generates a pair of files, *.vb/cs and *_base.vb/cs (e.g. *bll.vb/cs and *bll_base.vb/cs for business layer), and *.vb/cs classes inherits from *_base.vb/cs classes. The *.vb/cs classes are the user classes where you can write your own custom codes without the fear of them being overwritten. ASP.NET Maker will not overwrite these classes once they exists in the destination folder.

Writing custom codes to support new functionlity

In this example, we will show you how to write custom codes to check that the [Required Date] must be at least 7 days after the [Order Date] when adding a new order.

Open ASP.NET Maker, load the demo database and select the "Orders" table for generation.

To implement the logic, open the file Ordersbll.cs/Ordersbll.vb (in App_Code folder) with a text editor. You will see the following codes.

Viusal Basic

Namespace demo Public Class Ordersbll Inherits Ordersbll_base Public Sub New () End Sub End Class End Namespace
C#

namespace demo { public class Ordersbll : Ordersbll_base { public Ordersbll(){ } } }

Add the following codes to implement the required business logic.

Viusal Basic

Namespace demo Public Class Ordersbll Inherits Ordersbll_base Public Sub New () End Sub
Public Overloads Shared Function Inserting(ByVal row1 As Ordersrow) As Boolean ' Call the base class method to validate the data Dim valid As Boolean = Ordersbll_base.Inserting(row1) If (Not valid) Then Return False ' Check dates if required date and order date have values. If (row1.RequiredDate.HasValue AndAlso row1.OrderDate.HasValue) Then ' Calculate the date difference Dim diffTime As TimeSpan = row1.RequiredDate.Value.Subtract(row1.OrderDate.Value) ' if less than 7 days If (diffTime.Days < 7) Then ' Add Error Message Dim p As EW.Web.TableProfile = EW.Web.CustomProfile.GetTable(Share.ProjectName, "Orders") p.Message = "At least 7 days between order date and required date" pnlMessage.Visible = True Return False End If End If Return True End Function End Class End Namespace
C#

namespace demo { public class Ordersbll : Ordersbll_base { public Ordersbll(){ } public new static bool Inserting(Ordersrow row1) { // Call the base class method to validate the data bool valid = Ordersbll_base.Inserting(row1); if (!valid) return false; // Check dates if required date and order date have values. if (row1.RequiredDate.HasValue && row1.OrderDate.HasValue) { // Calculate the date difference TimeSpan diffTime = row1.RequiredDate.Value.Subtract(row1.OrderDate.Value); // if less than 7 days if (diffTime.Days < 7) { // Add Error Message EW.Web.TableProfile p = EW.Web.CustomProfile.GetTable(Share.ProjectName, "Orders"); p.Message = "At least 7 days between order date and required date"; pnlMessage.Visible = true; return false; } } return true; } } }

When user submits a new order with [Required Date] less than 7 days from [Order Date], an error message will be displayed.

 

 ©2004-2007 e.World Technology Ltd. All rights reserved.