I am working with WCF RIA services and have come across a sample using attributes:
[StringLength(10, ErrorMessage="Too long")]
public string FirstName { get; set; }
...
While attributes aren't restricted to WCF RIA, it reminded me of a question: why is declarative or attribute based programming perferable to coding a validation routine "the old fashioned way" ?
Thanks,
Scott
Because the constraint is discoverable without having to execute the code. With reflection you can access these constraints.
The biggest benefit is re-usability. It's great to be able to set it once (Name, Required, Regex, etc) and then use it in your WCF app, as well as your MVC app, and everything stays consistent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With