/// <summary>
        /// Create records from a collection
        /// </summary>
        /// <typeparam name="TType">Type of the collection</typeparam>
        /// <param name="values">The collection to create records from</param>
        /// <returns>Created records</returns>
        /// <exception cref="WebExtras.Core.InvalidUsageException"></exception>
        public static DatatableRecords From <TType>(ICollection <TType> values)
            where TType : class
        {
            List <string[]> data = new List <string[]>();

            if (values == null)
            {
                return(new DatatableRecords());
            }

            if (!values.Any())
            {
                return(new DatatableRecords());
            }

            Type t = typeof(TType);

            foreach (TType value in values)
            {
                List <KeyValuePair <int, string> > indexedValues = new List <KeyValuePair <int, string> >();
                PropertyInfo[] props = value.GetType().GetProperties();
                foreach (PropertyInfo prop in props)
                {
                    AOColumnAttribute[] attribs = (AOColumnAttribute[])prop.GetCustomAttributes(typeof(AOColumnAttribute), false);

                    if (attribs.Length == 0)
                    {
                        continue;
                    }

                    if (attribs.Length > 1)
                    {
                        throw new InvalidUsageException(
                                  string.Format("The property '{0}' on '{1}' can not have multiple decorations of AOColumn attribute", prop.Name, t.FullName));
                    }

                    // fact that we got here means that the current property is an AOColumn
                    IValueFormatter formatter = (IValueFormatter)(attribs[0].ValueFormatter == null ?
                                                                  new DefaultValueFormatter() :
                                                                  Activator.CreateInstance(attribs[0].ValueFormatter));

                    string val = formatter.Format(prop.GetValue(value, null), attribs[0].FormatString, value);

                    indexedValues.Add(new KeyValuePair <int, string>(attribs[0].Index, val));
                }

                data.Add(indexedValues.OrderBy(f => f.Key).Select(g => g.Value).ToArray());
            }

            DatatableRecords records = new DatatableRecords
            {
                iTotalDisplayRecords = data.Count,
                iTotalRecords        = data.Count,
                aaData = data.ToArray()
            };

            return(records);
        }
Exemple #2
0
        public void Format(FormatEntityContext <TEntity> context)
        {
            var value = _property.GetProperty(context.Entity);

            if (value.HasValue)
            {
                _formatter.Format(context.CreateValueContext(value));
            }
        }
Exemple #3
0
        /// <inheritdoc />
        public string?Format(object?value, Type valueType)
        {
            if (value == null)
            {
                return(null);
            }

            return(_baseFormatter.Format(value, valueType));
        }
Exemple #4
0
 /// <summary>
 /// Tries to format value with provided formatter.
 /// In case of error default formatting will be used.
 /// </summary>
 /// <typeparam name="T">Value type.</typeparam>
 /// <param name="formatter">Formatter.</param>
 /// <param name="value">Value to format.</param>
 /// <param name="valueType">Optional value type.</param>
 /// <returns>Formatted string.</returns>
 public static string?TryFormat <T>(this IValueFormatter formatter, T?value, Type?valueType = null)
 {
     try
     {
         return(formatter.Format(value, valueType ?? typeof(T)));
     }
     catch
     {
         return(DefaultToStringFormatter.Instance.Format(value, valueType ?? typeof(T)));
     }
 }
Exemple #5
0
        private String callFormatter(Object value, IContainer container)
        {
            IValueFormatter formatter = container.GetFormatter(Formatter);

            if (formatter == null)
            {
                return(null);
            }

            return(formatter.Format(value));
        }
        /// <inheritdoc />
        public string?Format(object?value, Type valueType)
        {
            if (value is ICollection collection)
            {
                return(collection.FormatAsTuple(
                           startSymbol: "[",
                           endSymbol: "]",
                           formatValue: item => _valueFormatter.Format(item, typeof(object))));
            }

            return(null);
        }
Exemple #7
0
        public void Format(FormatEntityContext <TEntity> context)
        {
            var value = _property.GetProperty(context.Entity);

            if (value.HasValue)
            {
                if (value.Slice is ParsedTextSlice)
                {
                    context.Append(value.Slice);
                }
                else
                {
                    _formatter.Format(context.CreateValueContext(value));
                }
            }
        }
Exemple #8
0
        private string FormatValue(string propValue)
        {
            if (String.IsNullOrEmpty(Formatter))
            {
                return(propValue == null ? null : String.Format(Format, propValue));
            }

            Type            formatterType = Type.GetType(Formatter);
            IValueFormatter formatter     = Activator.CreateInstance(formatterType) as IValueFormatter;

            if (formatter == null)
            {
                return(String.Format(Format, propValue));
            }

            return(formatter.Format(propValue));
        }
Exemple #9
0
        public void Format(FormatEntityContext <TEntity> context)
        {
            var value = _property.GetProperty(context.Entity);

            if (value.HasValue)
            {
                for (int i = 0;; i++)
                {
                    Value <TValue> currentValue;
                    if (!value.TryGetValue(i, out currentValue) || !value.IsPresent)
                    {
                        break;
                    }

                    if (value.HasValue)
                    {
                        _formatter.Format(context.CreateValueContext(currentValue));
                    }
                }
            }
        }
Exemple #10
0
 public static string Format <TValue>(IValueFormatter <TValue> formatter, TValue value, Nparams param)
 {
     return(formatter.Format(value, param));
 }
        private static string Format(object value, FormattingContext context, FormatChild formatChild)
        {
            IValueFormatter firstFormatterThatCanHandleValue = Formatters.First(f => f.CanHandle(value));

            return(firstFormatterThatCanHandleValue.Format(value, context, formatChild));
        }
Exemple #12
0
 /// <inheritdoc />
 public string?SerializeValue(Type type, object?value)
 {
     return(_valueFormatter.Format(value, type));
 }
        private static string FillPlaceholder(string valueSource, string format, object model, IValueFormatter formatter)
        {
            var property = model.GetType().GetProperty(valueSource);

            if (property == null)
            {
                throw new InvalidOperationException(string.Format("Property '{0}' does not exist", valueSource));
            }

            var value = property.GetValue(model);

            if (value == null)
            {
                return "";
            }

            return formatter.Format(value, format);
        }
 string FormatForQueryString(object v)
 {
     return(formatter.Format(v));
 }
Exemple #15
0
 public RegisterValueFormatter With <T>(IWriteValue <T> value)
 {
     _value = _formatter.Format(value.Expected);
     return(this);
 }