public static TableViewModel DtoMapper <T>(PaginatedEntitiesResult <T> entitiesResult, params TableRowActionViewModel[] actions) { Type dtoType = typeof(T); TableViewModel tableViewModel = new TableViewModel(); var properties = dtoType.GetProperties(); List <TableCellAttribute> dtoAttributes = new List <TableCellAttribute>(); foreach (var property in properties) { if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { dtoAttributes.Add((TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault()); } } List <string> tableHeaders = dtoAttributes.OrderBy(x => x.Order).Select(x => x.Name).Distinct().ToList(); tableHeaders.ForEach(x => { tableViewModel.Header.AddCell(x); }); if (entitiesResult.EntitiesCount > 0) { foreach (var entity in entitiesResult.Entities) { TableRowViewModel tableRow = new TableRowViewModel(); foreach (var property in properties) { if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { TableCellAttribute propertyAttribute = (TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault(); tableRow.AddCell(propertyAttribute.Order, property.GetValue(entity), propertyAttribute.Type); } } foreach (var action in actions) { List <string> parsedParameters = new List <string>(); foreach (var parameter in action.RawParameters) { if (parameter.StartsWith("[", StringComparison.OrdinalIgnoreCase) && parameter.EndsWith("]", StringComparison.OrdinalIgnoreCase)) { string propertyName = parameter.Substring(1, parameter.Length - 2); string propertyValue = entity.GetType().GetProperty(propertyName).GetValue(entity).ToString(); parsedParameters.Add(propertyValue); } else { parsedParameters.Add(parameter); } } tableRow.AddAction(action, parsedParameters); } tableViewModel.AddRow(tableRow); } } tableViewModel.SetPagination(entitiesResult.CurrentPage, entitiesResult.Pages); return(tableViewModel); }
public static TableViewModel DtoMapper <T>(PaginatedEntitiesResult <T> entitiesResult, params TableRowActionViewModel[] actions) { Type dtoType = typeof(T); TableViewModel tableViewModel = new TableViewModel(); var properties = dtoType.GetProperties(); List <TableCellAttribute> dtoAttributes = new List <TableCellAttribute>(); string defaultOrderPropertyName = null; FilterOrderType? defaultOrderType = null; var tableCellAttributePropertyNames = new List <string>(); var sortablePropertyAttributes = new List <SortablePropertyAttribute>(); foreach (var property in properties) { var defaultOrderAttribute = (TableDefaultOrderPropertyAttribute)property.GetCustomAttributes(typeof(TableDefaultOrderPropertyAttribute), false).FirstOrDefault(); if (defaultOrderAttribute != null) { defaultOrderPropertyName = property.Name; defaultOrderType = defaultOrderAttribute.OrderType; } if (property.GetCustomAttributes(typeof(SortablePropertyAttribute), false).Length > 0) { var orderPropertyAttribute = (SortablePropertyAttribute)property.GetCustomAttributes(typeof(SortablePropertyAttribute), false).First(); tableCellAttributePropertyNames.Add(property.Name); sortablePropertyAttributes.Add(orderPropertyAttribute); } else if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { sortablePropertyAttributes.Add(null); tableCellAttributePropertyNames.Add(null); } if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { dtoAttributes.Add((TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault()); } } SortTableCellAttributesAndPropertyNamesByOrderWeight(dtoAttributes, tableCellAttributePropertyNames, sortablePropertyAttributes); var tableCellNames = dtoAttributes .Select(x => x.Name) .Distinct() .ToList(); for (var i = 0; i < tableCellNames.Count; i++) { var propertyName = tableCellAttributePropertyNames[i]; var tableCellName = tableCellNames[i]; var orderPropertyAttribute = sortablePropertyAttributes[i]; if (orderPropertyAttribute != null) { if (propertyName == defaultOrderPropertyName) { tableViewModel.Header.AddCell(tableCellName, orderPropertyAttribute.OrderArgument, true, defaultOrderType); } else { tableViewModel.Header.AddCell(tableCellName, orderPropertyAttribute.OrderArgument, false); } } else { tableViewModel.Header.AddCell(tableCellName); } } if (entitiesResult.EntitiesCount > 0) { foreach (var entity in entitiesResult.Entities) { TableRowViewModel tableRow = new TableRowViewModel(); foreach (var property in properties) { if (property.GetCustomAttributes(typeof(EntityIdentifierAttribute), false).Length > 0) { tableRow.Identifier = property.GetValue(entity)?.ToString(); } if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { TableCellAttribute propertyAttribute = (TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault(); if (propertyAttribute?.Type == TableCellType.Flag) { tableRow.AddCell( propertyAttribute.Order, property.GetValue(entity), propertyAttribute.Type, propertyAttribute.Editable, property.Name, propertyAttribute.TextForTrueValue, propertyAttribute.TextForFalseValue); } else { tableRow.AddCell( propertyAttribute.Order, property.GetValue(entity), propertyAttribute.Type, propertyAttribute.Editable, property.Name, property.PropertyType.IsEnum ? property.PropertyType : null); } } } foreach (var action in actions) { List <string> parsedParameters = new List <string>(); foreach (var parameter in action.RawParameters) { if (parameter.StartsWith("[", StringComparison.OrdinalIgnoreCase) && parameter.EndsWith("]", StringComparison.OrdinalIgnoreCase)) { string propertyName = parameter.Substring(1, parameter.Length - 2); string propertyValue = entity.GetType().GetProperty(propertyName)?.GetValue(entity).ToString(); parsedParameters.Add(propertyValue); } else { parsedParameters.Add(parameter); } } tableRow.AddAction(action, parsedParameters); } tableViewModel.AddRow(tableRow); } } tableViewModel.SetPagination(entitiesResult.CurrentPage, entitiesResult.Pages); return(tableViewModel); }