正如标题所述,我很想知道,这是否为我的验证类别提供了从我的模型中获取所有财产的好想法。 理想的情况是,我要这样做,因为有些领域需要10+其他领域来核实它是否有效。 页: 1 还是这样,使模式和验证者彼此相联吗? 我指的是什么,这里就是一个例子。 然而,这部法律并没有发挥作用,因为它提供了无限的 lo!
Class User
Private m_UserID
Private m_Validator
Public Sub Class_Initialize()
End Sub
Public Property Let Validator(value)
Set m_Validator = value
m_Validator.Initialize(Me)
End Property
Public Property Get Validator()
Validator = m_Validator
End Property
Public Property Let UserID(value)
m_UserID = value
End property
Public Property Get UserID()
UserID = m_Validator.IsUserIDValid()
End property End Class
Class Validator
Private m_User
Public Sub Class_Initialize()
End Sub
Public Sub Initialize(value)
Set m_User = value
End Sub
Public Function IsUserIDValid()
IsUserIDValid = m_User.UserID > 13
End Function End Class
Dim mike : Set mike = New User
mike.UserID = 123456 mike.Validator = New Validator
Response.Write mike.UserID
如果我有权,而且这是一个好的想法,那么我怎么能够走头,与获得财产用户信息数据库一道固定住处?
谢谢。
Solution
<!-- #include file = "../lib/Collection.asp" -->
<style type="text/css">
td { padding: 4px; }
td.error
{
background: #F00F00;
}
td.warning
{
background: #FC0;
}
</style>
<%
Class UserModel
Private m_Name
Private m_Age
Private m_Height
Public Property Let Name(value)
m_Name = value
End Property
Public Property Get Name()
Name = m_Name
End Property
Public Property Let Age(value)
m_Age = value
End Property
Public Property Get Age()
Age = m_Age
End Property
Public Property Let Height(value)
m_Height = value
End Property
Public Property Get Height()
Height = m_Height
End Property
End Class
Class NameValidation
Private m_Name
Public Function Init(name)
m_Name = name
End Function
Public Function Validate()
Dim validationObject
If Len(m_Name) < 5 Then
Set validationObject = New ValidationError
Else
Set validationObject = New ValidationSuccess
End If
validationObject.CellValue = m_Name
Set Validate = validationObject
End Function
End Class
Class AgeValidation
Private m_Age
Public Function Init(age)
m_Age = age
End Function
Public Function Validate()
Dim validationObject
If m_Age < 18 Then
Set validationObject = New ValidationError
ElseIf m_Age = 18 Then
Set validationObject = New ValidationWarning
Else
Set validationObject = New ValidationSuccess
End If
validationObject.CellValue = m_Age
Set Validate = validationObject
End Function
End Class
Class HeightValidation
Private m_Height
Public Function Init(height)
m_Height = height
End Function
Public Function Validate()
Dim validationObject
If m_Height > 400 Then
Set validationObject = New ValidationError
ElseIf m_Height = 324 Then
Set validationObject = New ValidationWarning
Else
Set validationObject = New ValidationSuccess
End If
validationObject.CellValue = m_Height
Set Validate = validationObject
End Function
End Class
Class ValidationError
Private m_CSSClass
Private m_CellValue
Public Property Get CSSClass()
CSSClass = "error"
End Property
Public Property Let CellValue(value)
m_CellValue = value
End Property
Public Property Get CellValue()
CellValue = m_CellValue
End Property
End Class
Class ValidationWarning
Private m_CSSClass
Private m_CellValue
Public Property Get CSSClass()
CSSClass = "warning"
End Property
Public Property Let CellValue(value)
m_CellValue = value
End Property
Public Property Get CellValue()
CellValue = m_CellValue
End Property
End Class
Class ValidationSuccess
Private m_CSSClass
Private m_CellValue
Public Property Get CSSClass()
CSSClass = ""
End Property
Public Property Let CellValue(value)
m_CellValue = value
End Property
Public Property Get CellValue()
CellValue = m_CellValue
End Property
End Class
Class ModelValidator
Public Function ValidateModel(model)
Dim modelValidation : Set modelValidation = New CollectionClass
Validate name
Dim name : Set name = New NameValidation
name.Init model.Name
modelValidation.Add name
Validate age
Dim age : Set age = New AgeValidation
age.Init model.Age
modelValidation.Add age
Validate height
Dim height : Set height = New HeightValidation
height.Init model.Height
modelValidation.Add height
Dim validatedProperties : Set validatedProperties = New CollectionClass
Dim modelVal
For Each modelVal In modelValidation.Items()
validatedProperties.Add modelVal.Validate()
Next
Set ValidateModel = validatedProperties
End Function
End Class
Dim modelCollection : Set modelCollection = New CollectionClass
Dim user1 : Set user1 = New UserModel
user1.Name = "Mike"
user1.Age = 12
user1.Height = 32
modelCollection.Add user1
Dim user2 : Set user2 = New UserModel
user2.Name = "Phil"
user2.Age = 18
user2.Height = 432
modelCollection.Add user2
Dim user3 : Set user3 = New UserModel
user3.Name = "Michele"
user3.Age = 32
user3.Height = 324
modelCollection.Add user3
Validate all models in the collection
Dim modelValue
Dim validatedModels : Set validatedModels = New CollectionClass
For Each modelValue In modelCollection.Items()
Dim objModelValidator : Set objModelValidator = New ModelValidator
validatedModels.Add objModelValidator.ValidateModel(modelValue)
Next
%>
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>Height</td>
</tr>
<%
Dim r, c
For Each r In validatedModels.Items()
%><tr><%
For Each c In r.Items()
%><td class="<%= c.CSSClass %>"><%= c.CellValue %></td><%
Next
%></tr><%
Next
%>
</table>
这种做法虽然不完美,但比我开始的好。 基本上,我决定使用矫正器。 我的下一步是,最有可能将因诺特(Init)职能从每个职能中删除,代之以“Sto Model()”功能或某种功能。 这样,每个验证都能够利用我模式中的所有财产。
感谢大家。