English 中文(简体)
我的验证人是否应当接触我的整个模式?
原标题:Should my validator have access to my entire model?

正如标题所述,我很想知道,这是否为我的验证类别提供了从我的模型中获取所有财产的好想法。 理想的情况是,我要这样做,因为有些领域需要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>

Which produces Solution image

这种做法虽然不完美,但比我开始的好。 基本上,我决定使用矫正器。 我的下一步是,最有可能将因诺特(Init)职能从每个职能中删除,代之以“Sto Model()”功能或某种功能。 这样,每个验证都能够利用我模式中的所有财产。

感谢大家。

最佳回答

我认为,让有效者验证整个模式是正确的。 为了打破无限的循环,你可以向有效方传递价值。

Public Property Get UserID()
     UserID = m_Validator.IsUserIDValid(m_userID)
End property 

// in Validator
Public Function IsUserIDValid(userID)
    IsUserIDValid = userID > 13
End Function

或者,如果你更喜欢cap,那么你可以在未经验证的情况下增加获取财产的友好功能。

Public Property Get UserID()
     UserID = m_Validator.IsUserIDValid()
End property 

Friend Function GetUserID()
   GetUserID = m_userID
End Function

// in Validator
Public Function IsUserIDValid()
    // "private" access - to get the unvalidated property
    IsUserIDValid = m_user.GetUserID > 13
End Function

这样做的第三个办法是将你的物体与鉴定分开。 基础班界定了所有适当的物,未作验证。 之后,你界定了一个儿童类别,以补充确认:

class User
    Private m_userID
    Public Property Get UserID()
         UserID = m_userID
    End property 
End Class

class ValidatedUser inherits User
   Public Overrides Property Get UserID()
       if (m_userID<15)
           // handle invalid case, e.g. throw exception with property that is invalid
       UserID = m_userID
   End Property

   Public Function Validate()
      class-level validation
   End Function
End Class

最终变更使用代表团将基本用户特性与经验证的用户特性分开。 我们使用户成为一种抽象的类别,因为我们必须执行——一个得到验证,一个没有执行。

Class MustInherit User
   Public MustInherit Property Get UserID()
End Class

  A simple implementation of User that provides the properties
Class DefaultUser Inherits User
   Private m_UserID
   Public Overrides Property Get UserID()
      UserID = m_UserID
   End Property   
End Class

Class ValidatedUser Inherits User
   private Validator m_validator
   private User m_User

   Public Property Let Validator(value)
        Set m_Validator = value
        m_Validator.Initialize(m_User)
          note that validator uses m_User - this breaks the infinite recursion
    End Property

   Public Overrides Property Let UserID(value)
      m_User.UserID = value;
   End Property

   Public Overrides Property Get UserID()
      UserID = m_validator.IsUserValid();
   End Property
End Class   

在最后一个例子中,ValidatedUser认为与你原来的法典相似,但关键区别在于ValidatedUser本身没有任何财产价值,它代表所有财产获得者进入User物体。 经销商使用那些提供简单特性而未经验证的烟雾器,从而停止了无限的再入侵。

目前,在收回财产时进行验证。 我认为,这样做是因为你想要验证所使用数据,避免在分配财产时出现瞬间验证错误。 除了财产鉴定之外,你还可能要界定一种“全标”的验证方法,用以检查贵物体的所有财产,特别是那些涉及多重财产限制的财产。 例如,如果你有限制A+B+C <50,则将AB和C作为单独的特性加以核对,就会导致这一状况(A+B+C<50)受到3次评价,这是不必要的,并且也令人困惑,因为这一错误将出现在一个特定财产上,而当时它确实是一个与所有3个财产有关的问题。 你的标的验证人可以一度检查这一状况,并标明所有3种特性都无效的错误。

以上所有内容都对用户类别进行验证,使客户能够在不担心验证的情况下使用用户。 这种做法有好处,也有缺点。 好处是透明度——客户可以使用用户物体,在不明确要求的情况下在幕后得到验证。 倒数是,它与你的模型有着非常紧密的联系。 另一种办法是与用户物体完全分开验证。 这不仅证明有罪,而且还规定了“全标”的验证。 E.g.

  User is now a simple class (like DefaultUser above  
  with just properties, no validation  
Class UserValidator

   Public Function Validate(user)
       validate the given user object, return a list of
       validation errors, each validation error object
       that describes the property or properties
       that caused the validation error and why it s an error     
       E.g.  
     Dim ve As ValidationError 
     ve = new ValidationError
     ve.obj = user;     the object that failed validation
     ve.property = "userID"
     ve.msg = "userId must be < 15"
       potentially put several of these in a list and return to caller     
   End
End Class

任何操纵守则 用户在进行变革后必须明确叫Validate,但这通常不是一个问题,控制水平比自动控制要好得多。 (根据我的经验,青年几乎总是不得不在某个时候放弃“自动”行动,因为这些行动是徒劳的。)

我的写本超过了我的意图。 我希望这是有益的!

PS:我没有做很多的VB,因此,请概括一下偶然的辛迪加错误。 我是联络处的一位方案家,因此我知道这些原则是正确的。 我刚刚注意到“流层”标签——一些例子使用在古典版中可能无法提供的特征,尽管作为最后例子,单列的标签法应当按古典正典正文加以罚款。

问题回答

我通常确定一个验证整个模型的验证人;在这种情况下,我将有一个使用者类别,其方法是接受用户和回报验证结果,其中包括一个验证错误清单。

这使得你能够改变用户类别的实施,而不会影响验证(例如,在你增加新财产时,你不必为瓦莱德人类别增加新的方法,如果你想要改变用户信息数据库的验证方式,则改变方法签名等)。





相关问题
How to import excel file in web page useing Classic ASP?

We are trying to open an existing excel file (2003) from server location in a web page and save it again in the same location using following syntax. Set ExcelReportApp = CreateObject("Excel....

What s the best method to do paging in my ASP page

What s the best method to do paging in my ASP page when displaying a list of items? I knew that there is no equivalent to MySQL s LIMIT clause present in SQL Server, either 2000 or 2005. How can I ...

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

Response.Redirect HTTP status code

Why is it that ASP/ASP.NET Response.Redirect uses a HTTP-302 status code ("Moved Temporarily") even though in most cases a HTTP-301 status code ("Moved Permanently") would be more appropriate?

form inside form serialize problem

I am trying to submit a form inside another form because I will need first form s outcome in the second form. I tried using form serialize as advised in some other threads. Problem here is, I dont ...

热门标签