In addition to field-level verification, there may be more advanced business rules that cannot be expressed in a single column that contain different entities or concepts, such:
· If a product is marked as "disabled", its unit price cannot be modified.
· The place of residence of an employee must be the same as that of his/her supervisor.
· If a product is the only product provided by a supplier, it cannot be marked as "disabled"
In ASP. NET 20., The BLL class should always verify the business rules of the application. These verifications can be directly added to their methods.
Imagine that our business rules indicate that if a product is the only product of a given supplier, it cannot be marked as "disabled ". That is to say, if product X is the only product we purchased from supplier Y, we cannot mark it as disabled. However, if the vendor Y provides us with A total of three products, namely A, B, and C, we can mark any or all of them as "disabled ". Strange business rules, right? But the commercial rules are usually different from what we usually feel.
To apply this business rule in the UpdateProducts method, we should first check whether Discontinued is set to true. If so, call GetProductsBySupplierID to see how many products we have purchased from this supplier. If we only purchased this product from this supplier, we will throw an ApplicationException.
- Public BoolUpdateProduct (StringProductName,Int? SupplierID,Int? CategoryID,StringQuantityPerUnit,
- Decimal? UnitPrice,Short? UnitsInStock,Short? UnitsOnOrder,Short? ReorderLevel,
- BoolDiscontinued,IntProductID)
- {
- Northwind. ProductsDataTable products = Adapter. GetProductByProductID (productID );
- If(Products. Count = 0)
- // If no match is found, false is returned.
- Return False;
-
- Northwind. ProductsRow product = products [0];
-
- // Business Rule Check-the only product provided by a supplier cannot be deactivated
- If(Discontinued)
- {
- // Obtain all the products we obtain from this supplier
- Northwind. ProductsDataTable productsBySupplier = Adapter. GetProductsBySupplierID (product. SupplierID );
-
- If(ProductsBySupplier. Count = 1)
- // This is the only product we have obtained from this supplier.
- Throw NewApplicationException ("You cannot mark a product as discontinued if its only product purchased from a supplier");
- }
-
- Product. ProductName = productName;
- If(SupplierID =Null) Product. SetSupplierIDNull ();ElseProduct. SupplierID = supplierID. Value;
- If(CategoryID =Null) Product. SetCategoryIDNull ();ElseProduct. CategoryID = categoryID. Value;
- If(QuantityPerUnit =Null) Product. SetQuantityPerUnitNull ();ElseProduct. QuantityPerUnit = quantityPerUnit;
- If(UnitPrice =Null) Product. SetUnitPriceNull ();ElseProduct. UnitPrice = unitPrice. Value;
- If(UnitsInStock =Null) Product. SetUnitsInStockNull ();ElseProduct. UnitsInStock = unitsInStock. Value;
- If(UnitsOnOrder =Null) Product. SetUnitsOnOrderNull ();ElseProduct. UnitsOnOrder = unitsOnOrder. Value;
- If(ReorderLevel =Null) Product. SetReorderLevelNull ();ElseProduct. ReorderLevel = reorderLevel. Value;
- Product. Discontinued = discontinued;
-
- // Update product records
- IntRowsAffected = Adapter. Update (product );
-
- // If a record is updated, true is returned; otherwise, false is returned.
- ReturnRowsAffected = 1;
- }
Respond to verification errors in the presentation layer
When we call The BLL class from the presentation layer, we can decide whether to handle an exception that may be thrown or let it be directly thrown to ASP. NET, which will cause an error event of HttpApplication ). When using the BLL class, if you want to handle an exception programmatically, we can use the try... catch Block, as shown in the following example:
- ProductsBLL productLogic =NewProductsBLL ();
- // Update the product information with ProductID 1
- Try
- {
- // This operation will fail because we try to use a UnitPrice smaller than 0
- ProductLogic. UpdateProduct ("Scott's Tea", 1, 1,Null,-14 m, 10,Null,Null,False, 1 );
- }
- Catch(ArgumentException AE)
- {
- Response. Write ("There was a problem :"+ AE. Message );
- }
We will see in the following tutorial that when you insert, modify, or delete operation data using a data Web Control, the exception thrown from BLL can be handled directly in an Event Handler without using try... Catch Block to wrap the code.
Summary
An application with a good architecture has a clear hierarchy, and each layer encapsulates a specific role. In the first article of this tutorial, we created a data access layer using a Typed Dataset. In this article, we created a business logic layer, it consists of a series of classes in App_Code and calls the corresponding methods in DAL. The BLL class implements the field-level and business-Level Logic for our applications. Apart from creating an independent BLL, as we have done in this section, another option is to use the partial class to extend the methods in TableAdapter. However, using this technology does not allow us to rewrite existing methods, nor separate Our DAL and BLL clearly enough.
After completing the DAL and BLL, we are ready to start processing the presentation layer. In the next tutorial, we will briefly introduce some data access topics and define a consistent page presentation for the entire tutorial.
- How to deploy the asp.net mvc program in IIS6.0
- Use Winform to build the asp.net mvc Framework
- Programming idea of ASP. NET Session failure
- ASP. NET Session state storage
- Understand ASP. NET Web Application Models