Fun with Attribute Based Programming – Extending Enumerations

It seems that a lot of people have heard about attribute based programming, but never really get a chance to use it.  I started a thread over on GDN looking for different things people have done with it, but to my surprise most haven’t added it to their set of tools.  So I was asked to document one of the ways that I use attribute based programming. 

In the data access layer framework that I developed I had 2 goals in mind, encapsulate the gory details of the data provider that we are using with generic classes, and try to eliminate the need for duplicating code over and over again.  One thing that was very repetitive was how we were creating the Parameters for our Command objects.  We were looking for a way to eliminate the need for creating a utility class that has a static method for each type of parameter that was input or output from a stored proc.  We had about 100 different parameters, and the utility class was looking a bit unwieldy.  While talking to one of the developers on the project about the possible applications of attribute based programming he asks if we could use it to solve our problem with the parameter utility class.  Sounded perfect to me, so I asked him to give it a shot, and pretty soon we had a much better solution to the utility class problem, plus I thought it was a great way to explain attribute based programming.

If you want to see what we did, I wrote an article detailing it, which you can get here.  Since adopting the attribute based programming model we have found several places in our code where the use of meta data would make out code much easier to develop and support.  Any time I have pieces of related static data that are more than just name/value pairs, we extend enums with custom attributes so that we can use the simplicity of enums, but have them carry more than 2 pieces of data.

DonXML

[Listening to: Somewhere On Fullerton - Allister ] 

2 Comments

  • Very interesting idea. I've never thought of adding attributes to an enum for some odd reason. Just recently I had a scenario where it may have been useful. Anyway, I'm using attribute based programming to make a "entity object framework" that can dynamically generate "data manager" assemblies. Hopefully I'll post something soon on how I'm doing it. Anyway, great entry/article!

  • We've got a generic reporting framework that sees data generation for each report encapsulation in an object belonging to some level in an inheritance hierarchy. The report classes have properties tagged with attributes so that the generic filter form can determine which properties should be exposed as what filters to the user, and how they should presented (e.g. pick list, data driven pick list, etc.)





    Great article!

Comments have been disabled for this content.