网络编程
位置:首页>> 网络编程>> Asp编程>> 建立三层结构的ASP应用程序(3)

建立三层结构的ASP应用程序(3)

作者:佚名  发布时间:2009-01-21 19:41:00 

标签:结构,asp,程序,微软

下面我们再创建一个类Employees,并填加如下方法:

首先请注意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注册完毕。

4.编写ASP程序   





<HTML><Body> 
  <p>Qualified Employee List</p> 
  <table border=1 cellspacing=0 cellpadding=0> 
  <tr> 
   <td>Employee ID</td> 
   <td>Name</td> 
   <td>Gender</td> 
   <td>Score</td> 
  </tr> 
  <% 
   set oEmployees=server.createobject("Test.Employees") 
   oEmployees.GetQualifiedList 
   set oEmployee=oEmployees.GetFirst() 
   do while not oEmployee is nothing 
  %> 
  <tr> 
   <td><%=oEmployee.EMPLID%></td> 
   <td><%=oEmployee.Name%></td> 
   <td><%=oEmployee.Gender%></td> 
   <td><%=oEmployee.Score%></td> 
  </tr> 
  <% 
     set oEmployee=oEmployees.GetNext() 
   loop 
  %> 
  </table> 
  </body></html> 

注意在ASP中创建类实例的语法set oEmployees=server.createobject("Test.Employees"),其中Test是DLL的名字,Employees是类的名字; 当然,如果一个函数的返回值是一个对象,类似set oEmployee=oEmployees.GetFirst()这样的语法也是可以的。
至此,一个完整的三层结构的应用程序已经完成了,让我们看以下,如果把"合格"的定义改为:只有成绩进入前100名才算合格,程序需要做那些修改。事实上,如果你的数据库系统是SQL Server,你只需把SQL语句改为:
sql="select top 100 EMPLID from Employee order by Score desc" 就已经可以了,即使为了跨数据库系统的兼容性,我们也只需要对GetQualifiedList做如下修改:

...... 
  sql="select EMPLID from Employee order by Score desc" 
  with rs 
   .open sql,conn,1,3 
   if .eof and .bof then 
     exit sub 
   else 
     i=1 
     do while (not .eof) and (i<=100) 
       dim oEmployee as new Employee 
       oEmployee.Create trim(.Fields("EMPLID")) 
       colQualifiedList.Add oEmployee 
       set oEmployee=nothing 
       i=i+1 
     loop 
   end if 
   .close 
  end with 
  ... 

然后把DLL重新编译,注册就可以了,ASP程序完全不必修改。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com