Posted on June 30, 2008 10:26 by lbrown

Ever wonder how that magical tag called ‘[Serializable]’ placed at the top of a class enables it to be deconstructed into XML that can be tranmitted through firewalls or persisted in a files or database tables? Not to get too technical but attribute classes are embedded into the tagged classes’ metadata at compilation and can be reflected on to perform any function you desire. 

The following example provides a real world scenario where custom attributes can enhance one’s  coding experience.  I can’t tell you how many times I have to use stored procedures that have a million parameters each having the capacity to be null.  Think of a search procedure that has ten criteria.  If I was coding this with the SqlCommand class, I would have to write a method that creates each SqlParameter and populates it with DBNull or some value. There are over ninety nine combinations in this scenario.  That really cuts into my ebay time.

If I wrote an attribute that could turn a class into a SqlCommand with populated values and could be serialized in session or view state and reduce my time coding, life would be wonderful.  In addition, if I wrote a tool that generated the code for the attributed class, my evil plan to do no work would be complete!

I have attached a solution that contains all the code mentioned above.

AtrributeSolution.zip (51.43 kb)



Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted on June 25, 2008 10:15 by lbrown

Since I can remember, helper classes have been the backbone of coding since they comprise all the utilities that are missing in the .Net framework.  The downside is that one must keep track of all the assemblies, namespaces and various implementations as the toolset grows.  This can be a daunting task when considering each client’s library and the exhaustive resources available in the developer community. Wouldn’t be nice if we could simply extend the functionality of our existing libraries?

.Net 3.5 now allows classes to be extended by using a specific type of method declaration.

//The key work 'this' is included at the beginning 
//of the parameter list with the type to be 
//extended.  You can then do any type of implementation you like 
public static void DoSomething(this string str) 
{   
}

The following attached example illustrates taking a simple helper class used for encrypting strings and converting it into an extension that becomes part of the .Net framework. 

ExtensionExample.zip (94.54 kb)



Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5