您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > 创建业务逻辑层9030167617
简介在教程一中创建的数据访问层(DAL)将数据访问逻辑与表示逻辑清晰地分离开来。然而,尽管DAL从表示层中清晰地分离出数据访问层细节,它却并没有实施任何可能采用的业务规则。例如,我们想让我们的应用程序在Discontinued字段设为1时禁止对Products表的CategoryID或SupplierID字段的修改,还有,我们可能想实施一些资历规则以便禁止发生这样的情况:雇员被其后入职的另一雇员所管理。另一种常见的情形是授权–可能只有处于特定职位的用户可以删除产品或更改UnitPrice值。通过本教程,我们可以了解怎样将业务规则集中到在表示层与DAL之间充当数据交互中介的业务逻辑层(BLL)中。在真实的应用程序中,BLL应作为一个单独的类库项目而实现。然而,为了简化项目结构,在这些教程中,我们以App_Code文件夹下的一系列的类来实现BLL。图1展示了表示层、BLL和DAL之间的结构关系。图1:BLL将表示层与数据访问层分隔开来并且实施业务规则。步骤1:创建BLL类我们的BLL将由四个类组成,分别对应DAL中不同的TableAdapter。每个BLL类都具有一些方法,这些方法可以从DAL中该类对应的TableAdapter中检索、插入、更新或删除数据并应用相应的业务规则。为了更清楚地区分DAL的相关类与BLL的相关类,我们在App_Code文件夹下创建两个子文件夹:DAL和BLL。创建时,只需右健单击SolutionExplorer中的App_Code文件夹并选择NewFolder。创建了这两个文件夹后,将教程一中创建的TypedDataSet移动到DAL子文件夹中。然后,在BLL子文件夹中创建四个BLL类文件。为此,右键单击BLL子文件夹,选择AddaNewItem,然后选择Class模板。将这四个类分别命名为ProductsBLL、CategoriesBLL、SuppliersBLL和EmployeesBLL。图2:在App_Code文件夹中添加四个新类接下来让我们在每个类中添加一些方法,这些方法只是简单地封装教程一中为TableAdapters定义的方法。目前,这些方法只是对DAL中内容的直接调用,稍后我们会返回到这些方法中来添加任何所需的业务逻辑。注意:如果您当前使用的是VisualStudioStandardEdition或以上版本(即,当前使用的不是VisualWebDeveloper),您可以使用ClassDesigner以可视的方式随意设计自己的类。有关VisualStudio中该新特性的详细信息,请参见ClassDesignerBlog。对于ProductsBLL类,总共需要添加七个方法:GetProducts()–返回所有产品。GetProductByProductID(productID)–返回具有指定产品ID的产品。GetProductsByCategoryID(categoryID)–返回指定种类中的所有产品。GetProductsBySupplier(supplierID)–返回来自指定供应商的所有产品。AddProduct(productName,supplierID,categoryID,quantityPerUnit,unitPrice,unitsInStock,unitsOnOrder,reorderLevel,discontinued)–通过传入值将一个新产品插入到数据库中;返回新插入记录的ProductID值。UpdateProduct(productName,supplierID,categoryID,quantityPerUnit,unitPrice,unitsInStock,unitsOnOrder,reorderLevel,discontinued,productID)–通过传入值更新数据库中的一个现有产品;如果正好更新了一行则返回true,否则返回false。DeleteProduct(productID)–从数据库中删除指定产品。ProductsBLL.csusingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingNorthwindTableAdapters;[System.ComponentModel.DataObject]publicclassProductsBLL{privateProductsTableAdapter_productsAdapter=null;protectedProductsTableAdapterAdapter{get{if(_productsAdapter==null)_productsAdapter=newProductsTableAdapter();return_productsAdapter;}}[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select,true)]publicNorthwind.ProductsDataTableGetProducts(){returnAdapter.GetProducts();}[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select,false)]publicNorthwind.ProductsDataTableGetProductByProductID(intproductID){returnAdapter.GetProductByProductID(productID);}[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select,false)]publicNorthwind.ProductsDataTableGetProductsByCategoryID(intcategoryID){returnAdapter.GetProductsByCategoryID(categoryID);}[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select,false)]publicNorthwind.ProductsDataTableGetProductsBySupplierID(intsupplierID){returnAdapter.GetProductsBySupplierID(supplierID);}[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert,true)]publicboolAddProduct(stringproductName,int?supplierID,int?categoryID,stringquantityPerUnit,decimal?unitPrice,short?unitsInStock,short?unitsOnOrder,short?reorderLevel,booldiscontinued){//CreateanewProductRowinstanceNorthwind.ProductsDataTableproducts=newNorthwind.ProductsDataTable();Northwind.ProductsRowproduct=products.NewProductsRow();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;//Addthenewproductproducts.AddProductsRow(product);introwsAffected=Adapter.Update(products);//Returntrueifpreciselyonerowwasinserted,//otherwisefalsereturnrowsAffected==1;}[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update,true)]publicboolUpdateProduct(stringproductName,int?supplierID,int?categoryID,stringquantityPerUnit,decimal?unitPrice,short?unitsInStock,short?unitsOnOrder,short?reorderLevel,booldiscontinued,intproductID){Northwind.ProductsDataTableproducts=Adapter.GetProductByProductID(productID);if(products.Count==0)//nomatchingrecordfound,returnfalsereturnfalse;Northwind.ProductsRowproduct=products[0];product.ProductName=productName;if(supplierID==null)product.SetSupplierIDNull();elseproduct.SupplierID=supplierID.Value;if(categoryID==n
本文标题:创建业务逻辑层9030167617
链接地址:https://www.777doc.com/doc-1638822 .html