Getting All Valid Enum Values in ASP.NET Web API

One of the commenters on my blog posed an interesting question on my article about serializing enumerations in Web API: He wanted to know how someone who is querying a Web API might know what possible values a given enumeration has. I didn't answer that in the post he commented on, so I'll do that now. How do we tell consumers what possible values an enumeration in a Web API app has? Let's say we have the following enums: public... Read more >

Using T4 Templates to Generate Enums From Database Lookup Tables

The major project we've been working on had a non-unusual requirement: take these SQL database tables and convert them to enumerations we can use over a service. A quick Google search found some code that did exactly what we wanted, so of course we (ok, I) copied-and-pasted that code into our solution and BAM, it worked like a charm. Problem was, when someone asked me to explain what it did, I couldn't. I can't stand not understanding something, especially not... Read more >

Serializing Enums in ASP.NET Web API

Coming from a WCF world, I frequently used a contract-sharing trick that allowed both client and server to use the same set of DataContracts. This was especially important when we starting using Enums to better describe what certain values meant because the client and server could now both inherently have the same meaning for the same value. Of course, Web API is not the same beast as WCF, and we cannot have anything resembling a "contract" to share between client... Read more >

Best Practices: Fight Code Ambiguity with Enumerations

I've written before about the idea that code needs to have a reason to exist. Right alongside that idea is another I frequently find myself having to be reminded of: code must have a clear, explicit meaning. Let me clarify what I mean by that (irony alert!). I think that a reason to exist and meaning are two distinct ideas. In my mind, having meaning gives the code purpose and importance (much like it does for humans). Funny thing is,... Read more >