Trim String with Dapper.NET

c#, dapper, sql

Solution

I didn't like the idea of modifying Dapper directly. I decided to solve the problem by creating an extension method to wrap Dapper's and just reflect over the result and trim all string properties.

public static class DapperExtensions {
    public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) {
        var dapperResult = SqlMapper.Query<T>(cnn, sql, param, transaction, buffered, commandTimeout, commandType);
        var result = TrimStrings(dapperResult.ToList());
        return result;
    }

    static IEnumerable<T> TrimStrings<T>(IList<T> objects) {
        //todo: create an Attribute that can designate that a property shouldn't be trimmed if we need it
        var publicInstanceStringProperties = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType == typeof (string) && x.CanRead &&  x.CanWrite);
        foreach (var prop in publicInstanceStringProperties) {
            foreach (var obj in objects) {
                var value = (string) prop.GetValue(obj);
                var trimmedValue = value.SafeTrim();
                prop.SetValue(obj, trimmedValue);
            }
        }
        return objects;
    }

    static string SafeTrim(this string source) {
        if (source == null) {
            return null;
        }
        return source.Trim();
    }
}

Crucial to my solution (since I wanted to use the same name as Dapper) is how extension method resolution works, which you can read about here.

Problem

I have been using `Dapper.NET` for a while now. I was just wondering if it's possible to get Dapper to trim strings as it assigns them to the properties of the object. I currently use `LTRIM(RTRIM(fieldname))` in the SQL, and / or `value.Trim()` in the property setter. I am however working with a legacy database that uses chars instead of varchar, and I was wondering if there was a way to reduce my time of having to trim everything. I had a go myself by editing the source code of dapper but ended up breaking other mappings etc so gave in. Just wondered if anyone had any suggestions that could reduce this overhead. (I may be missing something very simple!) I am working with C# 3.5 by the way.

Original source

Related problems