mardi 19 février 2019

How to trim all strings from a complex object returned with dapper

I am working with a legacy database, within this database, the data get assigned the maximum length of the column. if the string data is shorter, it will automaticly fill in whitespaces at the end.

What i'm trying to do is trim all these ending whitespaces with every query i do.

i figured one of the better ways would be making an extension method for dapper query using reflection.

But i can't seem to get it to work.

parent entity:

public class Person: BaseEntity
   {
       public Identification Identification { get; set; }
       public Address Address { get; set; }
       public Contact Contact { get; set; }
       public Family Family { get; set; }
       public ICollection<Payment> Payments { get; set; }
   }

example of child entity:

 public class Address: BaseEntity
    {
        public string Street { get; set;  }
        public int Number { get; set; }
        public string BoxNumber { get; set; }
        public int ZipCode { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }


Now i do my join query like this:


var result = _db.QueryTrim<dynamic>(sql, new { userid = id })
                .Select(p => new Person()
                {
Identification = IdentificationMapper.MapToIdentificationEntity(p),
                    Address = AddressMapper.MapToAddressEntity(p)}).First();

i am trying to implement something like this but i can't get it to work with query

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();
    }
}

In the end i want to trim all string that come out of the database. At the moment i'm tunnelvisioning on the dapperextension, but if there is any better way. Please let me know.





Aucun commentaire:

Enregistrer un commentaire