Example #1
0
        private IEnumerable <TEntity> MapResponse(LogAnalyticsResponse response) // TODO: move to seperate mapper
        {
            // Response Mapping ====================================
            if (response?.Tables.IsNullOrEmpty() == false)
            {
                var accessors = TypeAccessor.Create(typeof(TEntity));
                var table     = response.Tables[0];
                var keys      = table.Columns.Safe().Select(c => c
                                                            .ColumnName.Safe().Replace("LogProperties_", string.Empty).SliceTillLast("_")).ToList();

                if (!table.Rows.IsNullOrEmpty())
                {
                    foreach (var values in table.Rows)
                    {
                        var result = Factory <TEntity> .Create();

                        var properties = new DataDictionary();
                        foreach (var key in keys.Ignore(new[] { "TenantId", "SourceSystem", "TimeGenerated", "ConnectionId" }))
                        {
                            var value = values[keys.IndexOf(key)];
                            if (value != null && !string.IsNullOrEmpty(value.ToString()))
                            {
                                properties.AddOrUpdate(key, value);
                            }
                        }

                        // dynamicly map all specified properties defined in entityMaps
                        foreach (var item in this.entityMap)
                        {
                            try
                            {
                                accessors[result, item.EntityProperty] = properties.TryGetValue(item.DtoProperty);
                                //properties.Remove(item.TargetProperty); // TODO: remove only if no further mappings based on TargetProperty
                            }
                            catch (System.ArgumentOutOfRangeException)
                            {
                                // target property not found, fastmember cannot set it
                            }
                        }

                        yield return(result);
                    }
                }
            }
        }