public IEnumerable <T> GetObjects <T>(string predicate = null) where T : class, new() { var objectType = typeof(T); var result = new QueryCSVResponse(); try { var customObjectAttribute = objectType.GetCustomAttributes(typeof(RightNowCustomObjectAttribute), true).FirstOrDefault() as RightNowCustomObjectAttribute; if (customObjectAttribute == null) { throw new InvalidOperationException("The type provided is not a RightNow custom object type. Please use the RightNowCustomObjectAttribute to associate the proper metadata with the type"); } var query = string.Format("SELECT * from {0}.{1} {2}", customObjectAttribute.PackageName, customObjectAttribute.ObjectName, predicate ?? string.Empty); var request = new QueryCSVRequest(getClientInfoHeader(), query, 100, ",", false, true); var rightNowChannel = getChannel(); result = rightNowChannel.QueryCSV(request); } catch (Exception ex) { Logger.Logger.Log.Error(string.Format("Exception at calling GetObject >> {0}", ex.Message)); } return(materializeObjects <T>(result, objectType)); }
private IEnumerable <T> materializeObjects <T>(QueryCSVResponse result, Type objectType, int objectCount = 100) where T : class, new() { var entities = new List <T>(); if (result.CSVTableSet != null && result.CSVTableSet.CSVTables != null && result.CSVTableSet.CSVTables.Length > 0) { var table = result.CSVTableSet.CSVTables[0]; var columns = new List <string>(table.Columns.Split(',')); var mapping = new Dictionary <int, PropertyInfo>(); var properties = objectType.GetProperties(); foreach (var property in properties) { var attribute = property.GetCustomAttributes(typeof(RightNowCustomObjectFieldAttribute), true).FirstOrDefault() as RightNowCustomObjectFieldAttribute; if (attribute != null) { int position = columns.IndexOf(attribute.Name); if (position > -1) { mapping.Add(position, property); } } } foreach (var row in table.Rows) { var values = row.Split(','); var entity = new T(); foreach (var entry in mapping.Keys) { var propertyInfo = mapping[entry]; propertyInfo.SetValue(entity, Parse(values[entry], propertyInfo.PropertyType), null); } entities.Add(entity); } } return(entities); }