Click here to Skip to main content
15,912,294 members

Survey Results

Do you use Attributes in your .NET development?   [Edit]

Survey period: 17 Oct 2011 to 24 Oct 2011

Attributes provide a neat way to hide the plumbing. Do you use and/or create attributes yourself?

OptionVotes% 
Extensively626.31
Often17017.31
Occasionally27928.41
Rarely19419.76
Never787.94
I am unfamiliar with Attributes in .NET19920.26



 
GeneralIt's awesome when you figure its power Pin
Fabio Franco21-Oct-11 2:17
professionalFabio Franco21-Oct-11 2:17 
I never saw the need to create new attributes until one day I stumbled into a problem:

How can I validate and normalize the fields of my business objects before I try to insert them in the database? I scratched my head over the idea for a few days on how to create reusable validation for different fields in an easy and intuitive way.

Attributes came to mind with a big: "AHA!"

So what I did was create Normalization and Validation attributes. I create two abstract attribute classes (normalization and validation) and all the other attributes inherited from these two.

Then I created an abstract class as a base class for all my business objects. This abstract classes implemented Validate and Normalize methods. Both these methods performed reflection to read attributes of the derived class and as such I had to implement it only once for all classes.

Now with those prepared I could start marking my fields with the attributes. Some examples:

1 - FieldName: This marked a field with a name to be displayed to the user when validation fails in a way to display the user where the problem is.

1 - MaxStringLength: This validation attribute marked a field of the class with a max string length, so it would match the database. With that attribute I could do two things:
a) Ensure string does not get truncated to the database and inform the user of the problem
b) Set the maxLength of textBoxes once in a centralized way. If database change, I only had to change it in one place (the attribute value on the field of the class) and it would automatically reflect on the textboxes related to that field.

2 - UpperCase: This normalization atribute automatically converted all marked fields to uppercase

3 - Numeric: This ensured that legacy varchar fields on the database that should contain numbers only stayed that way.

4 - Fill: This normalization could fill a field that is marked with maxStringLength attribute with the desired character up to its maximum length in a desired direction. Example: Fill with zeroes to the left.


These are just a few examples on how attributes made my life so much easier and very intuitive. I could create a business object and all I had to do was to mark the fields with these very intuitive attributes and saved me a lot of coding. And I mean A VERY BIG LOT.

Because of that, the business rules on the classes could be shrank dramatically. And this is only the beginning. There's lot more you can do with them.
"To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

GeneralRe: It's awesome when you figure its power Pin
Rakesh Meel23-Oct-11 19:48
professionalRakesh Meel23-Oct-11 19:48 
GeneralYes, When in my first week of new contract Pin
raju melveetilpurayil20-Oct-11 11:54
professionalraju melveetilpurayil20-Oct-11 11:54 
GeneralAttributes are the undead citizens of the .net code world. Pin
dave.dolan20-Oct-11 8:50
dave.dolan20-Oct-11 8:50 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 5:40
professionalFabio Franco21-Oct-11 5:40 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 5:46
dave.dolan21-Oct-11 5:46 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:07
professionalFabio Franco21-Oct-11 6:07 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:11
dave.dolan21-Oct-11 6:11 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:21
professionalFabio Franco21-Oct-11 6:21 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:27
dave.dolan21-Oct-11 6:27 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:43
professionalFabio Franco21-Oct-11 6:43 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:45
dave.dolan21-Oct-11 6:45 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:49
professionalFabio Franco21-Oct-11 6:49 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
PIEBALDconsult21-Oct-11 9:38
mvePIEBALDconsult21-Oct-11 9:38 
GeneralReflection + Attributes = FUN Pin
milkplus18-Oct-11 5:36
milkplus18-Oct-11 5:36 
GeneralRe: Reflection + Attributes = FUN Pin
Fabio Franco21-Oct-11 5:35
professionalFabio Franco21-Oct-11 5:35 
GeneralMostly with enumerations Pin
PIEBALDconsult17-Oct-11 18:13
mvePIEBALDconsult17-Oct-11 18:13 
GeneralRe: Mostly with enumerations Pin
ThatsAlok17-Oct-11 20:37
ThatsAlok17-Oct-11 20:37 
Generalwhat?? Pin
Dennis E White17-Oct-11 10:26
professionalDennis E White17-Oct-11 10:26 
GeneralRe: what?? Pin
CDP180217-Oct-11 11:27
CDP180217-Oct-11 11:27 
GeneralRe: what?? Pin
Dennis E White17-Oct-11 11:35
professionalDennis E White17-Oct-11 11:35 
GeneralRe: what?? Pin
Stonkie17-Oct-11 11:54
Stonkie17-Oct-11 11:54 
GeneralRe: what?? Pin
Edward Steward3-Dec-11 23:15
Edward Steward3-Dec-11 23:15 
GeneralRe: what?? Pin
PIEBALDconsult17-Oct-11 18:09
mvePIEBALDconsult17-Oct-11 18:09 
GeneralRe: what?? Pin
Keith Barrow18-Oct-11 0:35
professionalKeith Barrow18-Oct-11 0:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.