Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of attribute based programming?

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

like image 401
Scott Davies Avatar asked Sep 02 '10 05:09

Scott Davies


2 Answers

Because the constraint is discoverable without having to execute the code. With reflection you can access these constraints.

like image 75
Marco Avatar answered Nov 15 '22 12:11

Marco


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.

like image 43
Chase Florell Avatar answered Nov 15 '22 12:11

Chase Florell