如何在 PLINQO 中捕获 BrokenRuleException?

发布于 2024-08-13 03:43:36 字数 845 浏览 10 评论 0原文

创建自定义规则

 static partial void AddSharedRules()
 {
            RuleManager.AddShared<Tag>(
                new CustomRule<String>(
                    "TagName",
                    "Invalid Tag Name, must be between 1 and 50 characters",
                    IsNullEmptyOrLarge));
 }

我通过添加到我的实体类来

。然后我添加了规则(如视频中所示,尽管视频已注明日期并且包含错​​误信息):

public static bool IsNullEmptyOrLarge( string value )
    {
        return (value == null
            || String.IsNullOrEmpty(value)
            || value.Length > 50);
    }

但现在我有了调用代码……

try    
{    
    // some code
}
catch ( CodeSmith.Data.Rules… ??? )
{

// I can’t add the BrokenRuleException object. It’s not on the list.
}

我有:分配、安全性和验证。

在 PLINQO 中捕获破坏规则异常的正确方法是什么?

I’ve created a custom rule by adding the

 static partial void AddSharedRules()
 {
            RuleManager.AddShared<Tag>(
                new CustomRule<String>(
                    "TagName",
                    "Invalid Tag Name, must be between 1 and 50 characters",
                    IsNullEmptyOrLarge));
 }

to my Entity class.

I then added the rule (as seen on the video, although the video is dated and has wrong information):

public static bool IsNullEmptyOrLarge( string value )
    {
        return (value == null
            || String.IsNullOrEmpty(value)
            || value.Length > 50);
    }

But now I have the calling code…

try    
{    
    // some code
}
catch ( CodeSmith.Data.Rules… ??? )
{

// I can’t add the BrokenRuleException object. It’s not on the list.
}

I have: assign, security and Validation.

What’s the correct way to catch broken rule exceptions in PLINQO?

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

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

发布评论

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

评论(1

樱娆 2024-08-20 03:43:36

这是您需要做的,首先在您的项目中添加对

System.ComponentModel.DataAnnotations

using CodeSmith.Data.Rules;

然后的

try
{
    context.SubmitChanges();
}
catch (BrokenRuleException ex)
{
    foreach (BrokenRule rule in ex.BrokenRules)
    {
        Response.Write("<br/>" + rule.Message);
    }
}

引用如果您想更改默认消息,那么您可以转到您的实体并将属性从 更改

[Required]

[CodeSmith.Data.Audit.Audit]
private class Metadata
{
    // Only Attributes in the class will be preserved.

    public int NameId { get; set; }

    [Required(ErrorMessage="please please please add a firstname!")]
    public string FirstName { get; set; }

您还可以使用这些类型的数据注释属性

    [StringLength(10, ErrorMessage= "The name cannot exceed 10 characters long")]
    [Range(10, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Characters are not allowed.")]
    public string FirstName { get; set; }

HTH

Here is what you need to do, first add a reference in your project to

System.ComponentModel.DataAnnotations

using CodeSmith.Data.Rules;

Then

try
{
    context.SubmitChanges();
}
catch (BrokenRuleException ex)
{
    foreach (BrokenRule rule in ex.BrokenRules)
    {
        Response.Write("<br/>" + rule.Message);
    }
}

If you want to change the default message then you can go to your entity and change the attribute from

[Required]

to

[CodeSmith.Data.Audit.Audit]
private class Metadata
{
    // Only Attributes in the class will be preserved.

    public int NameId { get; set; }

    [Required(ErrorMessage="please please please add a firstname!")]
    public string FirstName { get; set; }

You can also use these types of data annotation attributes

    [StringLength(10, ErrorMessage= "The name cannot exceed 10 characters long")]
    [Range(10, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Characters are not allowed.")]
    public string FirstName { get; set; }

HTH

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