When we work with DataSets, DataTables and other means of packaging data, it is hard to avoid littering code with literal strings that represent column names. This is not helped by our tools -- how hard would it have been for Microsoft to expose an enum for column names from a DataTable object in a generated typed DataSet? (And why is "strongly-typed DataSet" preferred over "typed DataSet"?)
Several years ago, when I first started using .NET, I created an database column enum and constants generator. Point it to a database and let it generate enums or constants (in C# or VB.NET) for every table and view, or pick the ones you want. It's part of the suite of tools I use to create the data layer and business layer in my applications.
It's a simple thing. So instead of writing code like this:
30 dRow["LastName"] = "Smith";
31 dRow["FirstName"] = "Bob";
I can write it like this:
30 dRow[CustomerColumn.LastName] = "Smith";
31 dRow[CustomerColumn.FirstName] = "Bob";
A typical application is going to have references to columns in many places:
- Assigning values to columns
- Persisting data
- Filtering (using the DataTable's .Select method or a DataView's .RowFilter property)
- Sorting
- Ad-hoc querying
- Data binding and other tasks above in the designer for a form or web control
GridViews and other designer-based code may still be a chore, but you're not making things worse because that's how they are whether or not the rest of your source code has hard-coded values.
Does "LastName" look nicer than CustomerColumn.LastName? Probably. Is "LastName" easier to type? I'd say so. But it creates a mess, one that too many developers are keen to accept, unfortunately. Keep in mind, what I'm talking about here is for developers who haven't adopted object relational mapping or other means of encapsulating and abstracting object data. If you're still using DataSets and DataTables, you need to consider the friction your are accepting by hard-coding string column names, primary keys, default values, filters and so forth.
No comments:
Post a Comment