Validator 中的 FluentValidation 自定义函数 Class
FluentValidation Custom Function within Validator Class
使用来自 FluentValidation 网站的 example,我正在使用我自己的 classes 将概念转换为 VB.NET。我的问题感兴趣的部分是 Must(BeOver18)
,它调用 protected
函数。请注意,此调用不会将参数传递给 BeOver18
:
public class PersonAgeValidator : AbstractValidator<Person> {
public PersonAgeValidator() {
RuleFor(x => x.DateOfBirth).Must(BeOver18);
}
protected bool BeOver18(DateTime date) {
//...
}
}
我在 VB.NET 中创建了自己的验证器 class
Public Class ContractValidator
Inherits AbstractValidator(Of ContractDTO)
Public Sub New()
RuleSet("OnCreate",
Sub()
RuleFor(Function(x) x.CustomerID).NotEmpty
' Compiler error here:
' BC30455 Argument not specified for parameter 'customerID'.....
RuleFor(Function(x) x.CustomerID).Must(CustomerExists)
End Sub
)
End Sub
Protected Function CustomerExists(customerID As Integer) As Boolean
Return CustomerService.Exists(customerID)
End Function
End Class
问题: VB.NET 中带有 .Must(CustomerExists)
的行给出了 "Argument not specified for parameter 'customerID'..." 编译器错误。 C# 示例不向 BeOver18
传递参数。我尝试了一个额外的匿名内联函数来尝试传递 ContractDTO.CustomerID,但它不起作用,因为它无法被识别:
' This won't work:
RuleFor(Function(x) x.CustomerID).Must(CustomerExists(Function(x) x.CustomerID))
我不知道 C# 示例如何在没有参数的情况下调用它的函数,但 VB.NET 转换不能。这是我需要帮助的地方。
您的 CustomerExists
函数需要被视为委托。为此,请更改以下内容:
原创
RuleFor(Function(x) x.CustomerID).Must(CustomerExists)
更新
RuleFor(Function(x) x.CustomerID).Must(AddressOf CustomerExists)
使用来自 FluentValidation 网站的 example,我正在使用我自己的 classes 将概念转换为 VB.NET。我的问题感兴趣的部分是 Must(BeOver18)
,它调用 protected
函数。请注意,此调用不会将参数传递给 BeOver18
:
public class PersonAgeValidator : AbstractValidator<Person> {
public PersonAgeValidator() {
RuleFor(x => x.DateOfBirth).Must(BeOver18);
}
protected bool BeOver18(DateTime date) {
//...
}
}
我在 VB.NET 中创建了自己的验证器 class
Public Class ContractValidator
Inherits AbstractValidator(Of ContractDTO)
Public Sub New()
RuleSet("OnCreate",
Sub()
RuleFor(Function(x) x.CustomerID).NotEmpty
' Compiler error here:
' BC30455 Argument not specified for parameter 'customerID'.....
RuleFor(Function(x) x.CustomerID).Must(CustomerExists)
End Sub
)
End Sub
Protected Function CustomerExists(customerID As Integer) As Boolean
Return CustomerService.Exists(customerID)
End Function
End Class
问题: VB.NET 中带有 .Must(CustomerExists)
的行给出了 "Argument not specified for parameter 'customerID'..." 编译器错误。 C# 示例不向 BeOver18
传递参数。我尝试了一个额外的匿名内联函数来尝试传递 ContractDTO.CustomerID,但它不起作用,因为它无法被识别:
' This won't work:
RuleFor(Function(x) x.CustomerID).Must(CustomerExists(Function(x) x.CustomerID))
我不知道 C# 示例如何在没有参数的情况下调用它的函数,但 VB.NET 转换不能。这是我需要帮助的地方。
您的 CustomerExists
函数需要被视为委托。为此,请更改以下内容:
原创
RuleFor(Function(x) x.CustomerID).Must(CustomerExists)
更新
RuleFor(Function(x) x.CustomerID).Must(AddressOf CustomerExists)