三、如何开发三层结构的ASP应用程序
ASP具有良好的扩充性,我们访问数据库时,采用的时ADO对象,访问文件时,采用的是文件系统对象(FSO),其实这时程序已经是三层结构的应用程序了,只不过由于是利用内置的对象而为意识到罢了。这些对象都遵循COM/ActiveX&&接口,因此我们自己开发的对象也要遵循这个&&接口。下面,我们就以上文提到的"合格"标准为例,演示如何创建自己的三层结构的ASP应用。
1、在数据库系统中建立如下数据库表:
Employee: EMPLID char (5) not null,
Name char (10) not null,
Gender char (1) not null,
Score int not null
此表存储员工信息和考试成绩,为简单起见,这里只包含工号,姓名和性别三项,并且只有一门考试,EMPLID为主键。
2、建立动态链接库
启动VB(这里以VB为例,你可以用你喜欢的任何支持ActiveX&&接口的开发工具开发),新建一工程,工程类型为ActiveX DLL。在工程中新建一个类,取名为Employee。你可以Class Builder可视化的向类中填加&&属性和方法,也可以直接手工编辑。首先填加EMPLID&&属性如下:
Private msEMPLID as string
Property Let EMPLID(sEMPLID as string)
msEMPLID=sEMPLID
End Property
Property Get EMPLID() as string
EMPLID=msEMPLID
End Property
一般地讲,每一个&&属性都应该有Property Let和Property Get两个方法,它们分别当向&&属性赋值和读取&&属性值时被调用。如果某个&&属性只被赋值而从不被读取(这种情况多发生在对应数据库表的主键的&&属性上),则Property Get方法可以省略。Property Let方法不能省略。你可以仿照上面的程序再建立Name,Gender和Score三个&&属性。然后创建如下方法:
Public Sub Create(EMPLID as string)
dim conn as new Connection
dim rs as new Recordset
dim sql as string
'Suppose that you create a DSN in the control panel, the connectionstring property
'can also be dsn-less string
conn.ConnectionString="dsn=dsnname;uid=username;password=pwd"
conn.open
sql="select * from Employee where EMPLID='" & EMPLID & "'"
with rs
.open sql,conn,1,3
if .eof and .bof then
exit sub
else
msEMPLID=trim(.Fields("EMPLID"))
msName=trim(.Fields("Name"))
msGender=trim(.Fields("Gender"))
msScore=.Fields("Score")
end if
.close
end with
set rs=nothing
conn.close
set conn=nothing
End Sub
这里根据EMPLID创建Employee对象,注意数据库中的值是赋给三个私有变量,而不是直接赋值给&&属性,如果你单步调试就会发现,给msEMPLID赋值会调用Property Let EMPLID,也就是给&&属性赋值。
下面我们再创建一个类Employees,并填加如下方法:
private colQualifiedList as new Collection
private mnCurrentIndex as integer
Public Sub GetQualifiedList()
dim conn as new Connection
dim rs as new Recordset
dim sql as string
'Suppose that you create a DSN in the control panel, the connectionstring property
'can also be dsn-less string
conn.ConnectionString="dsn=dsnname;uid=username;password=pwd"
conn.open
sql="select EMPLID from Employee where Score>=60 order by Score desc"
with rs
.open sql,conn,1,3
if .eof and .bof then
exit sub
else
do while not .eof
dim oEmployee as new Employee
oEmployee.Create trim(.Fields("EMPLID"))
colQualifiedList.Add oEmployee
set oEmployee=nothing
loop
end if
.close
end with
set rs=nothing
conn.close
set conn=nothing
End Sub
首先请注意VB中创建类实例的语法dim oEmployee as new Employee,后面会看到,在ASP中创建类实例的语法是不同的。这个方法检索成绩大于等于60的员工工号,并据此创建一个Employee对象,再将此对象加入私有的集合对象中。下面两个&&函数遍历集合中的元素:
Public Function GetFirst() as Employee
if colQualifiedList.count>0 then
mnCurrentIndex=1
set GetFirst=colQualifiedList.Item(1)
else
set GetFirst=nothing
end if
End Function
Public Function GetNext() as Employee
mnCurrentIndex=mnCurrentIndex+1
if mnCurrentIndex>colQualifiedList.count then
set GetNext=nothing
else
set GetNext=colQualifiedList.Item(mnCurrentIndex)
End if
End Function
也许你会说,为何不把集合声明Public,这样在ASP中不是可以直接引用吗?确实,这样也行得通,编程实现起来也更简单些,但是,这样做破坏了封装性原则。因为数据以何格式存储完全是商业逻辑层的事,与用户界面层无关,假设有一天你因为每种原因放弃了用集合来存储数据的设计,而改用数组或记录集(Recordset)来存储,那你只需要修改GetFirst和GetNext两个&&函数,用户界面层完全无需修改。
至此类文件创建完毕,将工程文件存为 test.vbp,选File菜单下的Make test.dll选项将其编译。
3、注册动态链接库
启动Web Server 上的Microsoft Transaction Server (Start--Windows NT Optionpack4--Internet Information Server--Internet Service Manager),展开Microsoft Transaction Server--Computer--My Computer--Package Installed,点鼠标右键选New--Package--Create Empty Package,输入包名Test(这里Test是任选的名字,不一定要与DLL同名),OK-Interactive User-the current Logon user--Finish。双击Test--Component,右键选Component-New-Component-Install New component(s)-- Add File,选择你刚编译好的DLL文件,MTS会发现DLL中有两个类Employee和Employees。至此DLL注册完毕。