【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
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
相关文章推荐
- Create Asp.net ActiverX Step by Step
- 【ASP.NET step by step】之一 Create a DAL
- ASP.NET组件编程step by step
- ASP.NET SignalR Basis Step by Step
- 【ASP.NET Step by Step】之五 Declarative Parameters
- ASP.NET组件设计Step by Step(8)
- ASP.NET MVC Preview 3 STEP BY STEP 文章管理实例(一)
- Step by Step 配置使用HTTPS的ASP.NET Web应用
- ASP.NET组件设计Step by Step(4)
- Create ActiveX in .NET Step by Step
- ASP.NET组件设计Step by Step(3)
- Step by step configure ASP.net SQL connection to use Membership providers
- [ASP.NET 设计模式] 用Visual Studio2010搭建一个简单的分层结构示例Step by Step —— 01 准备工作
- [转]Bootstrap 3.0.0 with ASP.NET Web Forms – Step by Step – Without NuGet Package
- 【ASP.NET Step by Step】之三 Master Pages and Site Navigation
- Create ActiveX in .NET Step by Step
- How To Create an ASP.NET HTTP Handler by Using Visual C# .NET
- Step by Step 配置使用HTTPS的ASP.NET Web应用[转载]
- SharePoint State Machine Workflow +ASP.NET ASPX Form step by step(1)-创建Workflow Content Type
- 【ASP.NET Step by Step】之 开篇 Scott Mitchell