您的位置:首页 > 编程语言 > ASP

【ASP.NET step by step】之二 Create a BLL

2008-11-30 14:36 351 查看
上一篇写的有些杂乱,有点事无巨细的感觉。这篇还是简洁为主。

1. BLL将TableAdapter里面的方法进一步包装,例如 ProductsBLL.GetProducts(),

另外各个方法应该有必要的逻辑,比如不同的人有不同的权限,返回的产品列表应该不一样,等等。。。

2. .NET 2.0增加了nullable types,在函数的参数列为 int?x 的形式,表示x可以为空

3. AddProduct方法的特殊之处。

A:

因为ProductsRow是继承于ADO.NET的DataRow,而DataRow没有默认的无参构造函数,为了创建一个ProductsRow的实例,我们必须先创建一个ProductsDataTable的实例,然后调用它的NewProductRow方法

public bool AddProduct(string productName, int? supplierID, int? categoryID, string quantityPerUnit,

52ProductsDataTable.ColumnChanging.cs

public partial class ProductsDataTable

{

public override void BeginInit()

{

this.ColumnChanging += ValidateColumn; //重写 实现绑定event handler

}

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);

}

}

}

进入UpdateProduct方法:

//....

product.ProductName = productName;

if (supplierID == null) product.SetSupplierIDNull(); else product.SupplierID = supplierID.Value;

//...

第一步,设置第一个参数,ProductName,程序进入NorthWind类的嵌入类ProductRow的ProductName这个property的set部分

set { this[this.tableProducts.ProductNameColumn] = value; } //value="Scott's Tea"

第二步,因为更改了column,产生ColumnChanging事件,触发ValidateColumn函数响应,进入ValidateColumn函数内部。

UpdateProduct其他的参数依照依次处理。 直到遇到设置unitprice = -14,

throw new ArgumentException("UnitPrice cannot be less than zero", "UnitPrice");

进入Catch{}, response.wirte()会将异常显示在页面开头部分。

There was a problem: UnitPrice cannot be less than zero Parameter name: UnitPrice
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: