using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; using Newtonsoft.Json; using PetaPoco; using Sony.Filtr.Contracts.Entities; namespace Sony.Filtr.Contracts { public class FiltrMapper : IMapper { public TableInfo GetTableInfo(Type pocoType) { return TableInfo.FromPoco(pocoType); } public ColumnInfo GetColumnInfo(PropertyInfo pocoProperty) { return ColumnInfo.FromProperty(pocoProperty); } public Func GetFromDbConverter(PropertyInfo targetProperty, Type sourceType) { if (targetProperty.PropertyType == typeof(CultureInfo)) { return x => new CultureInfo((string)x); } if (targetProperty.PropertyType == typeof(List)) { return x => { var value = (string)x; if (!string.IsNullOrWhiteSpace(value)) { return value.Split(',').ToList(); } return null; }; } if (targetProperty.PropertyType == typeof(object))// && Attribute.IsDefined(targetProperty, typeof(DynamicAttribute))) { return x => { var stringValue = (string)x; if (!string.IsNullOrWhiteSpace(stringValue)) { try { return JsonConvert.DeserializeObject(stringValue); } catch (Exception) //TODO: Catch specific exception { return null; } } return null; }; } if (targetProperty.PropertyType == typeof (SpotifyLink)) { return x => new SpotifyLink((string) x); } return null; } public Func GetToDbConverter(PropertyInfo sourceProperty) { if (sourceProperty.PropertyType == typeof(CultureInfo)) { return x => ((CultureInfo) x).Name; } if (sourceProperty.PropertyType == typeof(List)) { return x => { var value = (List) x; if (value != null) { return string.Join(",", value); } return null; }; } return null; } } }