为需要为此应用程序创建的每个 collection 重复这一过程。
collection 的实现
在大多数实现中,collection 是对 visual basic 的 collection 的一个封装,visual basic 的 collection 驻留在 collection 的私有部分中。在大多数情况下,collection 将包含以下方法:item()、count()、load()、 add()、delete() 和 update()。
下面是对每种方法的讨论。
item():
public property get item(byval index as variant) as cuserinfo
on error goto errorhandler
dim mintcodeid as integer
set item = nothing
if mcol is nothing then
goto cleanexit
end if
if mcol.count = 0 then
goto cleanexit
end if
if trim(index & "") = "" or index <= 0 then
goto cleanexit
else
'将索引转换为一个整数,否则 collection 不会工作
mintcodeid = cint(index)
set item = mcol.item(mintcodeid)
end if
<...>
end property
item() 返回一个存储在 collection 中的 cuserinfo。它首先进行默认条件检查:空、范围和类型,然后为该 class 设置一个引用。
此该函数可能因所用的内部存储模型的不同而有所不同。对于此样例实现的绝大部分而言,虽然使用 visual basic 的 collection,但样例代码确实包含了另一种存储模型的一个样例。
注意: 这是一个本文未涉及但却包括在实现中的 ecc 高级概念。
count():
public property get count() as variant
on error goto errorhandler
if mcol is nothing then
count = 0
else
count = mcol.count
end if
<...>
end property
count() 返回零或存储在内部的 class 的数目。
注意: 如果将“可变数组”用于内部存储机制,则可用像 ubound 这样的函数来确定项目数。
load():
friend function load(byval securitytoken as variant, byval _
filledstorage as variant) as variant
on error goto errorhandler
load = false
mvarsecuritytoken = securitytoken
set mcol = filledstorage
load = true
<...>
end function
load() 方法假定 engine 已经在内部存储中填入了必要的数据。它随后只是将数据赋给一个局部变量。允许 engine 填充内部存储提供了更大的灵活性,因为它知道原始请求。它同时也存储了 securitytoken 的一个副本,以供以后需要用 securitytoken 来进行处理的请求使用。
add():
public function add(byval userid as variant, byval usertypeid as _
variant, byval password as variant, byval name as variant) _
as variant
on error goto errorhandler
dim newtemp as cuserinfo
'设置默认返回值
add = -1
'检查参数值
if userid = "" or usertypeid = "" or password = "" _
or name = "" then
err.raise err_userinfofailedaddcheck, _
"coluserinfo.add proc", "you must specify values"
goto cleanexit
end if
if mcol is nothing then
set mcol = new collection
end if
set newtemp = new cuserinfo
with newtemp
'设置所需要的字段
.classstorage = true
.usernumber = 0
.userid = userid
.usertypeid = usertypeid
.name = name
<...>
.password = password
.securitytoken = securitytoken
.classstorage = false
end with
'将项目添加到 collection 的尾部
if mcol.count <> 0 then
mcol.add newtemp, , , mcol.count
else
mcol.add newtemp
end if
add = mcol.count
<...>
end function
add() 方法首先检查所传递的参数是否符合业务规则。随后创建一个用户 class (cuser),将默认值赋给特性,并将 cuser 添加到内部存储中。如果尚未创建内部 collection,则此方法同时创建内部 collection 的成员。此函数返回此 collection 中 class 的项目编号,以便调用者可以立即访问此项目。
delete():
注意: 请记住,此方法会通过网络访问外部源。