如何将 Castle Validator 与 Subsonic 生成的类一起使用?

发布于 2024-07-12 23:47:40 字数 111 浏览 7 评论 0原文

Castle Validator 使用属性来指定验证规则。 如何将它们与 Subsonic 生成的类(或任何无法定义属性的类)连接起来? 有没有一种方法可以在不使用属性方法的情况下以编程方式指定验证规则?

Castle Validator uses attributes to specify validation rules. How can you hook these up with Subsonic's generated classes (or any classes where you can't define the attributes on)? Is there a way to programatically specify validation rules without using the attribute method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野味少女 2024-07-19 23:47:40

我认为最好的方法是使用 MetadataType
它是一个DataAnnotations,让您的类拥有一对或类似的东西。 我不知道如何更好地解释它,所以,让我们看一下示例:

您首先需要将此指令添加到您的代码中:

using System.ComponentModel.DataAnnotations;

您应该创建生成的类的部分类,并使用指定该类具有 MetadataType


[MetadataType(typeof(UserMetadata))] 
public partial class User
{
}

然后使用城堡验证创建元数据类:


public class UserMetadata
{
    [ValidateNonEmpty]
    [ValidateLength(6, 24)]
    public string Username { get; set; }

    [ValidateNonEmpty]
    [ValidateLength(6, 100)]
    [ValidateEmail]
    public string Email { get; set; }

    [ValidateNonEmpty]
    [ValidateLength(6, 24)]
    public string Password { get; set; }
}

I think the best way to do that is using MetadataType.
It's a DataAnnotations that let's your class have like a pair or something like that. I don't know how to explain it better so, let's for the samples:

You first need to add this directive to your code:

using System.ComponentModel.DataAnnotations;

Them you should create the partial class of you generated class with an attribute specifying that this class has an MetadataType:


[MetadataType(typeof(UserMetadata))] 
public partial class User
{
}

Then you create your metadata class with your castle validation:


public class UserMetadata
{
    [ValidateNonEmpty]
    [ValidateLength(6, 24)]
    public string Username { get; set; }

    [ValidateNonEmpty]
    [ValidateLength(6, 100)]
    [ValidateEmail]
    public string Email { get; set; }

    [ValidateNonEmpty]
    [ValidateLength(6, 24)]
    public string Password { get; set; }
}
各自安好 2024-07-19 23:47:40

有几种方法可以做到这一点 - 属性是摩擦力最低的选项,但显然不能很好地处理生成的代码或更好地在代码中表达的多个属性的验证。

请查看以下链接,了解如何撰写此博客文章的一些指示:城堡验证器增强功能

如果您查看了城堡源代码,这些是一些很好的起点:

There are a few ways to do this - attributes is the lowest friction option, but obviously doesn't deal well with generated code or validation of multiple properties better expressed in code.

Take a look at the following link for some indications on how to do this blog post: Castle Validator Enhancements

If you have a look at the castle source code these are some good starting points:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文