Attributes And C#

After using c# for a while now for both front end development as well as back end development one of the most under used features that i have come across is attributes.

Attributes bring out one of the most important and one of the most under used parts of the .Net library .Yes what i am talking about is meta data. The stuff that makes your intellisense so easy to use and well as a number of other things in the library.

The one area that i have seen make use of this extensively is code generation. For example a generated business object class can have a attributes that defines required property’s in the class. Next implement a method that using reflection inspects the property’s of the class and whether each property has a required attribute. If the property has a required attribute ,check if it has a value.

Depending on whether a value is found or not a message or alert can be raised and a notification can be sent to the user. This in turn can initiate a system decision as to either stop processing or continue but still raise a warning.

Since a attribute can be defined on just about anything, from methods to classes we can take attributes to another level. This level or definition allows designers of systems to take the system to a more generic level in which  a single validation method is created per class and no matter how many new fields are added to the class as long as they are marked as required they will be checked in the validation command.

The other often over looked part about attributes is that they are still classes at heart.What this means is that you can store data in them. Granted they have some limitations (we wont get into that here) , but for the most part we can use this very efficiently.

The Scenario :

In a web app it is inevitable that you will create forms that perform CRUD statements via your web page. While there are plenty of code generators out there that will perform this for you, the inevitable is that they will have some shortcoming that you can not overcome.

The Proposed Solution :

Depending on the current state of the project this may or may not help. If it is the beginning or design phase of the project it may be wise to invest some time to and develop a code generator.

Keep in a mind that a code generator is not meant to cover every possible scenario in the beginning but to grow with the application. Also its a good idea to know when to generate code and when to hand develop the code.

As always there is no one soloution to all answers, you need to evaluate that if the attibutes will help you or just make the code more like bowl of spaghetti code. Also keep in mind that code should be developed for the maintainer , so while the attributes may make sense to you and your team , will the next developer that comes onto the team be able to pick it up with ease and use it for the rest of the project.

Keep your code clean and make sure you love what you do ;) , as always suggestions  are welcome.

~Jameel

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

2 Comments to “Attributes And C#”

  1. Blackhatseo 21 July 2008 at 6:45 am #

    Added. Nice work on this one. Btw, my blog is dofollow, stop by and grab a link. Walter

  2. Import from China 22 July 2008 at 5:03 pm #

    I came across this blog the other day and you got some great info here – thanks.


Leave a Reply