Пример #1
0
        /// <summary>
        /// Creates new property of type <typeparamref name="TResult"/> that evaluates its value as value of property <paramref name="property"/> and <paramref name="map"/> func.
        /// Map func can receive null values.
        /// </summary>
        /// <typeparam name="TSource">Source type.</typeparam>
        /// <typeparam name="TResult">Result type.</typeparam>
        /// <param name="property">Source property.</param>
        /// <param name="map">Function that maps value of type <typeparamref name="TSource"/> to type <typeparamref name="TResult"/>.
        /// TSource MayBeNull, TResult MayBeNull but Func at the current moment does not support nullability.</param>
        /// <param name="allowMapNull">By default false: does not calls <paramref name="map"/> if <paramref name="property"/> value is null.</param>
        /// <param name="configureSearch">Allows to reconfigure user search options for current call.</param>
        /// <returns>New property of type <typeparamref name="TResult"/>.</returns>
        public static IProperty <TResult> Map <TSource, TResult>(
            this IProperty <TSource> property,
            Func <TSource, TResult> map,
            bool allowMapNull = false,
            Func <SearchOptions, SearchOptions>?configureSearch = null)
        {
            (TResult, ValueSource) ConvertValue(IPropertyContainer container, SearchOptions search)
            {
                search = configureSearch?.Invoke(search) ?? search;
                IPropertyValue <TSource>?sourcePropertyValue = container.GetPropertyValue(property, search);

                if (sourcePropertyValue.HasValue())
                {
                    TSource sourceValue = sourcePropertyValue.Value;
                    bool    shouldMap   = !sourceValue.IsNull() || (sourceValue.IsNull() && allowMapNull);
                    if (shouldMap)
                    {
                        // TSource MayBeNull, TResult MayBeNull but Func at the current moment does not support nullability.
                        TResult resultValue = map(sourceValue);
                        return(resultValue, ValueSource.Calculated);
                    }
                }

                return(default(TResult), ValueSource.NotDefined);
            }

            return(new Property <TResult>(property.Name)
                   .With(description: property.Description, alias: property.Alias)
                   .WithCalculate(ConvertValue));
        }
Пример #2
0
        /// <inheritdoc />
        public string?Render(IPropertyContainer source)
        {
            if (CustomRender != null)
            {
                return(CustomRender(Property, source) ?? NullValue);
            }

            IPropertyValue <T>?propertyValue = source.GetPropertyValue(Property, SearchOptions);

            if (propertyValue.HasValue())
            {
                return(propertyValue.Value?.FormatValue() ?? NullValue);
            }

            return(NullValue);
        }
        /// <inheritdoc />
        public string Render(IPropertyContainer source)
        {
            if (CustomRender != null)
            {
                return(CustomRender(Property, source));
            }

            string textValue = NullValue;

            IPropertyValue <T> propertyValue = source.GetPropertyValue(Property, SearchOptions ?? Metadata.SearchOptions.ExistingOnlyWithParent);

            if (propertyValue.HasValue())
            {
                T value = propertyValue.Value;
                textValue = FormatValue?.Invoke(value, source) ?? DoDefaultFormatting(value);
            }

            return(textValue);
        }