开发者

Issues with inheritance and Validation Application Block's ObjectCollectionValidator

开发者 https://www.devze.com 2023-01-30 16:22 出处:网络
I\'m sorry if this has been asked before, I tried many combinations of keywords to describe the issue but was unable to find anything on Google or SO.

I'm sorry if this has been asked before, I tried many combinations of keywords to describe the issue but was unable to find anything on Google or SO.

The problem is simple: and I have a List of objects. I've marked this list with ObjectCollectionValidator so that objects get validated. Some of the objects in the list aren't, however, of type T, but of some derived type.

Those derived types have properties not present on their parents that need to be validated. They simply won't. I believe that VAB isn't checking inherited types when dealing with开发者_Go百科 ObjectCollectionValidator, but that is a flaw in my opinion, so I'm hoping I'm wrong.

By the way, I'm using EntLib's 4.1 (and while I wanted, I cannot switch to EntLib 5.0).


What you're witnessing is a design quirk of VAB 4.1 that has been solved in VAB 5.0. In VAB 5.0, when you decorate a collection property with a ObjectCollectionValidatorAttribute, you can leave out the actual type and in that case, the validator will validate objects by their actual type.

If you can't switch to VAB 5.0, the only thing you can do is write your own object collection validator. This isn't really difficult. Just look at the source of of the ObjectCollectionValidatorAttribute and ObjectCollectionValidator of VAB 5.0.

Good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号