Search This Blog

Monday, December 21, 2009

Using Microsoft Enterprise Library VAB with WCF services – Part 2

In Part 1 we looked at a simple solution on how to do service side validation for a simple WCF service and pass the resulting validation result back to the client.  In this article we’ll look at some other types of validators provided by the Enterprise Library and also touch the topic of rulesets and how they help.

So far we have looked at RangeValidator; let us look at some of its properties.

Bounds
The LowerBound and UpperBound of the validator are self-explanatory and denote the minimum and maximum value the validator can accept.

BoundType
The BoundType is synonymous to the bounds you have, it tells you whether the bounds are included, excluded or simply ignored.

Negated
If negated is set then we basically negate the rule, therefore in our example where we had the rangevalidator to accept only positive integers, setting negated=true would mean we only allow negative integers.  Kind of a redundant property in my opinion and simply complicates things.

Tag
Allows the developer to replace the name of the property which caused the validation fault to a custom string.  Useful when your property name is something like “accessDate” and you want to return “Access Date”.  Keep in mind that the custom Tag is only read when you have a custom message template.

MessageTemplate
Each validator provided by the Enterprise Library comes with its own message which gets returned on a validation fault, this can be customised by the developer.  For e.g. for the Range Validator we know (looking at the MSDN documentation) token {2} is the Tag, therefore our message template could be “{2} is invalid.” and we would get “Access Date is invalid.”

MessageTemplateResourceName/MessageTemplateResouceType
The following items come in handy when you have declared your messages in a resource file.

Ruleset
Allows the developer to target different validation rules depending on different criteria.  E.g. different clients may have different validation rules depending on different business logic.  This can be done by simply changing the config file to tell the validation block which ruleset to use as shown below:







To be continued…

No comments:

Post a Comment