Inevitably, developers will be involved in a project where the database is not SQL Server or Oracle; meaning, there are times when developers need to interact with data either on a mainframe or an AS400 (such is the case with James and I today). If you've had the pleasure of working with those backend platforms, you know that most of the time data is stored in fixed-width fields. For example, the field that contains a persons first name will be 50 characters in length, thus for the first name “DAVE” you'll get “DAVE” followed by 46 spaces.
There are various points along the way where you can easily trim those annoying spaces: the data access layer, the business logic, or you could even argue to let the UI do it because some think trimming extra spaces is a diplay issue. It doesn't really matter where you do the trimming, as long as it gets done. In the majority of cases, I've always felt it best to trim the data as soon as you get it in the data access layer, so that when I pass the data up to the business logic all is as it should be.
But typically, you have to trim each and every field one at a time. For example, you make your connection to the legacy backend, execute a command to populate a DataReader, then you might pull data from the DataReader so that you can populate an entity object, like an Address or Customer. And for every single field you pull from the DataReader you have to trim it; otherwise, your entity will contain the fixed-width data. I don't know about you, but that gets old real quick and is a real hassle, especially in any decently sized entity model that maps to a legacy backend.
So I've come up with a much more elegant solution that saves an immense amount of time and coding: create a Trim method in your base entity class. I'll tell you now that any well-designed entity model should have a single abstract base class that all other entities inherit from. Even if you don't need it right away, you will, trust me. If you don't put one in place, you will more than likely regret it later when it comes time to add functionality to each and every entity in your model.
OK, onto the code. Below is an abstract class named EntityBase that contains a protected method Trim. I made it protected to ensure that only classes that inherited from EntityBase could use it. The Trim method uses reflection to find all of the entity's public properties that are strings, gets the current value of each one, then updates each one's value to the trimmed version of the current value. Note that the return type of Trim is EntityBase, so you'll need to cast it appropriately to the actual entity type you're working with.
using System;
using System.Reflection;
public
abstract class EntityBase
{
protected EntityBase Trim()
{
Type t = this.GetType();
// We're only interested in trimming the public properties, so there
// is no need to set BindingFlags on the GetProperties call.
foreach (PropertyInfo pi in t.GetProperties())
{
// We only need to trim strings
if (pi.PropertyType == Type.GetType("System.String"))
{
object currentVal = pi.GetValue(this, null);
pi.SetValue(this, currentVal.ToString().Trim(), null);
}
}
return this;
}
}
So assuming you have an entity named Address that inherits from EntityBase, you can populate the Address from the DataReader (don't worry about trimming each field) and simply call Trim to get an updated Address object with all public string properties trimmed as they should be:
Address addr = GetAddressFromDataReader();
Address trimmedAddr = (Address)addr.Trim();
I don't know about you, but that's too damn easy NOT to use. Enjoy.
Print | posted on Thursday, October 13, 2005 1:07 PM