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 ]