具有继承性的企业库验证应用程序块
我正在使用企业库验证应用程序块。
抽象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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做了一个测试,这对于基类和抽象基类来说工作得很好。你能发布你的配置吗?
请注意,在处理字段时不要使用属性验证 XML(反之亦然)。
根据上面的示例,您的配置应如下所示:
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:
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:
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.