ASP. NET 2.0 data Tutorial: add business rules to the BLL class

Source: Internet
Author: User

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.

 
 
  1. Public BoolUpdateProduct (StringProductName,Int? SupplierID,Int? CategoryID,StringQuantityPerUnit,
  2. Decimal? UnitPrice,Short? UnitsInStock,Short? UnitsOnOrder,Short? ReorderLevel,
  3. BoolDiscontinued,IntProductID)
  4. {
  5. Northwind. ProductsDataTable products = Adapter. GetProductByProductID (productID );
  6. If(Products. Count = 0)
  7. // If no match is found, false is returned. 
  8. Return False;
  9.  
  10. Northwind. ProductsRow product = products [0];
  11.  
  12. // Business Rule Check-the only product provided by a supplier cannot be deactivated 
  13. If(Discontinued)
  14. {
  15. // Obtain all the products we obtain from this supplier 
  16. Northwind. ProductsDataTable productsBySupplier = Adapter. GetProductsBySupplierID (product. SupplierID );
  17.  
  18. If(ProductsBySupplier. Count = 1)
  19. // This is the only product we have obtained from this supplier. 
  20. Throw NewApplicationException ("You cannot mark a product as discontinued if its only product purchased from a supplier");
  21. }
  22.  
  23. Product. ProductName = productName;
  24. If(SupplierID =Null) Product. SetSupplierIDNull ();ElseProduct. SupplierID = supplierID. Value;
  25. If(CategoryID =Null) Product. SetCategoryIDNull ();ElseProduct. CategoryID = categoryID. Value;
  26. If(QuantityPerUnit =Null) Product. SetQuantityPerUnitNull ();ElseProduct. QuantityPerUnit = quantityPerUnit;
  27. If(UnitPrice =Null) Product. SetUnitPriceNull ();ElseProduct. UnitPrice = unitPrice. Value;
  28. If(UnitsInStock =Null) Product. SetUnitsInStockNull ();ElseProduct. UnitsInStock = unitsInStock. Value;
  29. If(UnitsOnOrder =Null) Product. SetUnitsOnOrderNull ();ElseProduct. UnitsOnOrder = unitsOnOrder. Value;
  30. If(ReorderLevel =Null) Product. SetReorderLevelNull ();ElseProduct. ReorderLevel = reorderLevel. Value;
  31. Product. Discontinued = discontinued;
  32.  
  33. // Update product records 
  34. IntRowsAffected = Adapter. Update (product );
  35.  
  36. // If a record is updated, true is returned; otherwise, false is returned. 
  37. ReturnRowsAffected = 1;
  38. }

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:

 
 
  1. ProductsBLL productLogic =NewProductsBLL ();
  2.  // Update the product information with ProductID 1 
  3.  Try 
  4. {
  5. // This operation will fail because we try to use a UnitPrice smaller than 0 
  6. ProductLogic. UpdateProduct ("Scott's Tea", 1, 1,Null,-14 m, 10,Null,Null,False, 1 );
  7. }
  8.  Catch(ArgumentException AE)
  9. {
  10. Response. Write ("There was a problem :"+ AE. Message );
  11. }

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.

  1. How to deploy the asp.net mvc program in IIS6.0
  2. Use Winform to build the asp.net mvc Framework
  3. Programming idea of ASP. NET Session failure
  4. ASP. NET Session state storage
  5. Understand ASP. NET Web Application Models

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.