Main ASP .NET Tutorial Index 

Tutorial 2: Creating a Business Logic Layer

 

Scott Mitchell

June 2006

Download the ASPNET_Data_Tutorial_2_CS.exe sample code.

Contents of Tutorial 2 (Visual C#)

Introduction
Step 1: Creating the BLL Classes
Step 2: Accessing the Typed DataSets Through the BLL Classes
Step 3: Adding Field-Level Validation to the DataRow Classes
Step 4: Adding Custom Business Rules to the BLL's Classes
Summary

 

Step 3: Adding Field-Level Validation to the DataRow Classes

Field-level validation are checks that pertains to the property values of the business objects when inserting or updating. Some field-level validation rules for products include:

  • The ProductName field must be 40 characters or less in length
  • The QuantityPerUnit field must be 20 characters or less in length
  • The ProductID, ProductName, and Discontinued fields are required, but all other fields are optional
  • The UnitPrice, UnitsInStock, UnitsOnOrder, and ReorderLevel fields must be greater than or equal to zero

These rules can and should be expressed at the database level. The character limit on the ProductName and QuantityPerUnit fields are captured by the data types of those columns in the Products table (nvarchar(40) and nvarchar(20), respectively). Whether fields are required and optional are expressed by if the database table column allows NULLs. Four check constraints exist that ensure that only values greater than or equal to zero can make it into the UnitPrice, UnitsInStock, UnitsOnOrder, or ReorderLevel columns.

In addition to enforcing these rules at the database they should also be enforced at the DataSet level. In fact, the field length and whether a value is required or optional are already captured for each DataTable's set of DataColumns. To see the existing field-level validation automatically provided, go to the DataSet Designer, select a field from one of the DataTables and then go to the Properties window. As Figure 4 shows, the QuantityPerUnit DataColumn in the ProductsDataTable has a maximum length of 20 characters and does allow NULL values. If we attempt to set the ProductsDataRow's QuantityPerUnit property to a string value longer than 20 characters an ArgumentException will be thrown.

Figure 4. The DataColumn Provides Basic Field-Level Validation

Unfortunately, we can't specify bounds checks, such as the UnitPrice value must be greater than or equal to zero, through the Properties window. In order to provide this type of field-level validation we need to create an event handler for the DataTable's ColumnChanging Event. As mentioned in the preceding tutorial, the DataSet, DataTables, and DataRow objects created by the Typed DataSet can be extended through the use of partial classes. Using this technique we can create a ColumnChanging event handler for the ProductsDataTable class. Start by creating a class in the App_Code folder named ProductsDataTable.ColumnChanging.cs.

Figure 5. Add a New Class to the App_Code Folder

Next, create an event handler for the ColumnChanging event that ensures that the UnitPrice, UnitsInStock, UnitsOnOrder, and ReorderLevel column values (if not NULL) are greater than or equal to zero. If any such column is out of range, throw an ArgumentException.

ProductsDataTable.ColumnChanging.cs

public partial class Northwind
{
   public partial class ProductsDataTable
   {
        public override void BeginInit()
         {
            this.ColumnChanging += ValidateColumn;
         }

         void ValidateColumn(object sender, DataColumnChangeEventArgs e)
         {
            if(e.Column.Equals(this.UnitPriceColumn))
            {
               if(!Convert.IsDBNull(e.ProposedValue) && (decimal)e.ProposedValue < 0)
               {
                  throw new ArgumentException("UnitPrice cannot be less than zero", "UnitPrice");
               }
            }
            else if (e.Column.Equals(this.UnitsInStockColumn) ||
                    e.Column.Equals(this.UnitsOnOrderColumn) ||
                    e.Column.Equals(this.ReorderLevelColumn))
            {
                if (!Convert.IsDBNull(e.ProposedValue) && (short)e.ProposedValue < 0)
                {
                    throw new ArgumentException(string.Format("{0} cannot be less than zero", e.Column.ColumnName), e.Column.ColumnName);
                }
            }
         }
   }
}

 

 

 

Go to top of page or next part of tutorial

 
eXTReMe Tracker