具有继承性的企业库验证应用程序块

发布于 2024-09-07 03:47:48 字数 150 浏览 7 评论 0原文

我正在使用企业库验证应用程序块。

抽象A类 { 公共 int 字段A; }

B 类:A { 我

将验证器添加到类型 B。我无法找到字段 fieldA,而且即使我在配置中输入它,验证也不会成功。 VAB 不考虑抽象类和继承的属性或字段吗?

I'm using Enterprise library Validation application block.

abstract Class A
{
public int fieldA;
}

class B:A
{
}

I add validators to type B. I'm not able to find the field fieldA and also even if I enter it in configuration, the validation does not succeed. Does VAB not take the abstract class and inherited properties or fields into account?

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

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

发布评论

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

评论(2

随遇而安 2024-09-14 03:47:49

我做了一个测试,这对于基类和抽象基类来说工作得很好。你能发布你的配置吗?

请注意,在处理字段时不要使用属性验证 XML(反之亦然)。

根据上面的示例,您的配置应如下所示:

  <validation>
    <type assemblyName="MyProject.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      name="MyProject.Test.B">
      <ruleset name="RuleSetB">
        <fields>
          <field name="fieldA">
            <validator lowerBound="1" lowerBoundType="Inclusive" upperBound="100"
              upperBoundType="Inclusive" negated="false" messageTemplate="fieldA must be between 1 and 100"
              messageTemplateResourceName="" messageTemplateResourceType=""
              tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
              name="Range Validator" />
          </field>
        </fields>
      </ruleset>
    </type>
  </validation>

I did a test and this is working fine for base classes and abstract base classes. Can you post your configuration?

Be careful that you are not using the property validation XML when you are dealing with fields (and vice versa).

Based on your sample above, your configuration should look like:

  <validation>
    <type assemblyName="MyProject.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      name="MyProject.Test.B">
      <ruleset name="RuleSetB">
        <fields>
          <field name="fieldA">
            <validator lowerBound="1" lowerBoundType="Inclusive" upperBound="100"
              upperBoundType="Inclusive" negated="false" messageTemplate="fieldA must be between 1 and 100"
              messageTemplateResourceName="" messageTemplateResourceType=""
              tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
              name="Range Validator" />
          </field>
        </fields>
      </ruleset>
    </type>
  </validation>
爱情眠于流年 2024-09-14 03:47:49

VAB 仅在使用属性时支持继承。基于配置的验证不支持继承(您可以在常见问题解答 此处)。 entlib 团队给出的解决方法是:

要解决此问题,您可以
复制验证规范
对于子类。

当然,这种解决办法很糟糕,因为对于基类中的每一个更改,您可能都必须将其复制到许多派生类型。这是脆弱且容易出错的。因此,我构建了一个解决方案,能够将验证从基类复制到实现。

代码太多,无法在 Stackoverflow 上发布,但您可以阅读我关于它的博客文章 这里

我希望这有帮助。

VAB supports inheritance only when using attributes. Inheritance is not supported for configuration based validation (as you can read in the FAQ here). The work around given by the entlib team is this:

To work around this issue, you can
replicate the validation specification
for the subclasses.

Of course this work around sucks, because for each and every change in a base class you possibly have to copy it to many derived types. This is brittle and error prone. Because of this I built a solution that is able to duplicate validations from base classes to implementations.

It is just too much code to post it here on Stackoverflow, but you can read my blog post about it here.

I hope this helps.

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