使用数据注释属性验证方法参数
与 VS2010/Silverlight 4 捆绑在一起的“Silverlight 业务应用程序”模板在其域服务类中的方法参数上使用 DataAnnotations,这些参数会自动调用:
public CreateUserStatus CreateUser(RegistrationData user,
[Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
[RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
[StringLength(50, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
string password)
{ /* do something */ }
如果我需要在 POCO 类方法中实现此功能,如何让框架调用验证或如何强制调用所有参数的验证(使用验证器或其他方式?)。
The "Silverlight Business Application" template bundled with VS2010 / Silverlight 4 uses DataAnnotations on method arguments in its domain service class, which are invoked automagically:
public CreateUserStatus CreateUser(RegistrationData user,
[Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
[RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
[StringLength(50, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
string password)
{ /* do something */ }
If I need to implement this in my POCO class methods, how do I get the framework to invoke the validations OR how do I invoke the validation on all the arguments imperatively (using Validator or otherwise?).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们是这样处理的:
我们有一个 ValidationProperty 类,它接受一个正则表达式,该表达式将用于验证值(您可以使用您想要的任何内容)。
ValidationProperty.cs
以下是我们创建验证属性的方法。请注意公共成员是一个字符串,但我私下使用的是“ValidationProperty”。
现在,每当设置该值时,业务层都会验证它是否是有效值。如果不是,它只会抛出一个新的 ValidationException(在 ValidationProperty 类中)。在您的 xaml 中,您需要设置 NotifyOnValidationError & ValidatesOnExceptions 为 true。
通过这种方法,您可能会有一个用于创建新“用户”的表单,并且每个字段每次设置时都会进行验证(我希望这是有意义的)。
这是我们用来在业务层进行验证的方法。我不确定这是否正是您正在寻找的,但我希望它有所帮助。
We have approached it like this:
We have a ValidationProperty class that takes in a RegularExpression that will be used to validate the value (you could use whatever you wanted).
ValidationProperty.cs
Here's how we would create a Validation property. Notice how the public member is a string, but privately I am using a 'ValidationProperty.'
Now, whenever the value is set, the business layer will validate that it's a valid value. If it's not, it will simply throw a new ValidationException (in the ValidationProperty class). In your xaml, you will need to set NotifyOnValidationError & ValidatesOnExceptions to true.
With this approach, you would probably have a form for creating a new 'User' and each field would valitate each time they set it (I hope that makes sense).
This is the approach that we used to get the validation to be on the business layer. I'm not sure if this is exactly what you're looking for, but I hope it helps.