Workflow Foundation 4.0 中设计时验证的模式是什么?

发布于 2024-08-13 06:12:23 字数 176 浏览 4 评论 0原文

我不清楚何时以及如何在 WF 4.0 中的自定义活动/自定义活动工厂/自定义设计器中进行验证。

我的活动中唯一提供验证错误支持的地方是 CacheMetadata 方法(我相信在设计过程中会多次调用该方法)。这是我应该进行验证的地方吗? ActivityDesigner 中是否支持验证?

I'm unclear about when and how I should be doing validation in my custom activities/custom activity factories/custom designers in WF 4.0.

The only place within my activity that seems to provide validation error support is within the CacheMetadata method (I believe this is called multiple times during the design process). Is this where I should be doing my validation? Is there support for validation within the ActivityDesigner?

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

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

发布评论

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

评论(2

梦醒时光 2024-08-20 06:12:24

是的,您的活动中的 CacheMetadata 是您应该进行验证的地方。验证是一个适用于工作流运行时和设计者的概念。 (您可以从以下事实中看出这一点:您仍然可以尝试运行具有验证错误的已编译或声明式工作流程,但这样做时会抛出异常。)

示例:(

protected override void CacheMetadata(ActivityMetadata metadata)
{
   if (this.Body == 0) metadata.AddValidationError(
     new ValidationError(
       "You forgot to supply a body for (this activity)",
       /*iswarning = */ true, 
       "Body"));
}

请参阅 ActivityMetadata.AddValidationError)

您的第二个问题是是否支持从 ActivityDesigner 端添加验证?答案是“不,不是真的” - 可以进行一些自定义 WPF 和数据绑定,在 ActivityDesigner 之上添加一些“自定义验证”,甚至可以使用 AttachedPropertiesService 在活动类上定义您自己的 SatisfiesConstraints 属性。但这是额外的工作,并且不会与运行时验证一致地配合,因此这是一个价值有限的想法。

Yes, CacheMetadata in your activity is where you should be doing your validation. Validation is a concept which applies to the workflow runtime as well as the designer. (You can see this from the fact that you can still attempt to run workflows, compiled or declarative, which have validation errors, but an exception will be thrown when you do.)

Example:

protected override void CacheMetadata(ActivityMetadata metadata)
{
   if (this.Body == 0) metadata.AddValidationError(
     new ValidationError(
       "You forgot to supply a body for (this activity)",
       /*iswarning = */ true, 
       "Body"));
}

(See ActivityMetadata.AddValidationError)

Your secondary question is is there support for adding validation from the ActivityDesigner side? The answer is 'no, not really' - it would be possible for to do some custom WPF and data binding which adds some 'custom validation' on top of the ActivityDesigner, maybe even use AttachedPropertiesService to define your own SatisfiesConstraints property on your activity class. But it's extra work, and it's not going to tie in with the runtime validation consistently, so it's a limited value idea.

雨落星ぅ辰 2024-08-20 06:12:24
public class SalesCommissionValidation : CodeActivity<decimal>
{
    public decimal NetSales { get; set; }
    public decimal Percentage { get; set; }

    protected override decimal Execute(CodeActivityContext context)
    {
        var commission = NetSales * (Percentage / 100);
        return commission;
    }

    protected override void CacheMetadata(CodeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);

        if (NetSales <= 0)
            metadata.AddValidationError("Sales cannot be less than 0!");
        else if (Percentage <= 0)
            metadata.AddValidationError("Sales percentgage cannot be less than 0!");
        else
        {
            if (Percentage > 20)
                metadata.AddValidationError(string.Format("Sales percentgage {0} cannot be greater than 20%", Percentage));
        }

    }
}
public class SalesCommissionValidation : CodeActivity<decimal>
{
    public decimal NetSales { get; set; }
    public decimal Percentage { get; set; }

    protected override decimal Execute(CodeActivityContext context)
    {
        var commission = NetSales * (Percentage / 100);
        return commission;
    }

    protected override void CacheMetadata(CodeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);

        if (NetSales <= 0)
            metadata.AddValidationError("Sales cannot be less than 0!");
        else if (Percentage <= 0)
            metadata.AddValidationError("Sales percentgage cannot be less than 0!");
        else
        {
            if (Percentage > 20)
                metadata.AddValidationError(string.Format("Sales percentgage {0} cannot be greater than 20%", Percentage));
        }

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