/// <summary>
        /// Sets property value and returns the same container.
        /// </summary>
        /// <param name="propertyContainer">MutablePropertyContainer.</param>
        /// <param name="property">Property to set.</param>
        /// <param name="value">Value to set.</param>
        /// <param name="valueSource">Value source.</param>
        /// <returns>The same container with changed property.</returns>
        public static IMutablePropertyContainer WithValueUntyped(this IMutablePropertyContainer propertyContainer, IProperty property, object?value, ValueSource?valueSource = default)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));
            property.AssertArgumentNotNull(nameof(property));

            propertyContainer.SetValueUntyped(property, value, valueSource);
            return(propertyContainer);
        }
コード例 #2
0
        /// <summary>
        /// Adds untyped validation to property metadata <see cref="IPropertyValidationRules"/>.
        /// </summary>
        /// <param name="property">Source property.</param>
        /// <param name="validationRule">Validation rule.</param>
        /// <returns>The same property.</returns>
        public static IProperty AddValidation(this IProperty property, IValidationRule validationRule)
        {
            property.AssertArgumentNotNull(nameof(property));
            validationRule.AssertArgumentNotNull(nameof(validationRule));

            return(property.ConfigureMetadata <IProperty, IPropertyValidationRules>(
                       createMetadata: CreatePropertyValidationRules,
                       configureMetadata: propertyValidation => propertyValidation.AddRule(validationRule)));
        }
        /// <summary>
        /// Sets property value and returns the same container.
        /// </summary>
        /// <typeparam name="TContainer">Property container type.</typeparam>
        /// <typeparam name="T">Property type.</typeparam>
        /// <param name="propertyContainer">MutablePropertyContainer.</param>
        /// <param name="property">Property to set.</param>
        /// <param name="value">Value to set.</param>
        /// <param name="valueSource">Value source.</param>
        /// <returns>The same container with changed property.</returns>
        public static TContainer WithValue <TContainer, T>(this TContainer propertyContainer, IProperty <T> property, T value, ValueSource?valueSource = default)
            where TContainer : IMutablePropertyContainer
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));
            property.AssertArgumentNotNull(nameof(property));

            propertyContainer.SetValue(property, value, valueSource);
            return(propertyContainer);
        }
コード例 #4
0
        /// <summary>
        /// Sets value for property.
        /// </summary>
        /// <typeparam name="T">Property type.</typeparam>
        /// <param name="property">Property.</param>
        /// <param name="value">Value to store.</param>
        /// <param name="valueSource">Value source.</param>
        /// <returns><see cref="IPropertyValue{T}"/> that holds value for property.</returns>
        public IPropertyValue <T> SetValue <T>(IProperty <T> property, T?value, ValueSource?valueSource = default)
        {
            property.AssertArgumentNotNull(nameof(property));

            var propertyValue = PropertyValueFactory.Default.Create(property, value, valueSource);

            SetValue(propertyValue);
            return(propertyValue);
        }
コード例 #5
0
        /// <summary>
        /// Sets <see cref="IPropertyValidationRule{T}"/>.
        /// Replaces property metadata <see cref="IPropertyValidationRules"/>.
        /// </summary>
        /// <typeparam name="T">Property value type.</typeparam>
        /// <param name="property">Source property.</param>
        /// <param name="validation">Property validation.</param>
        /// <returns>The same property.</returns>
        public static IProperty <T> SetValidation <T>(this IProperty <T> property, Func <IProperty <T>, IPropertyValidationRule <T> > validation)
        {
            property.AssertArgumentNotNull(nameof(property));
            validation.AssertArgumentNotNull(nameof(validation));

            IPropertyValidationRule <T> validationRule  = validation(property);
            IPropertyValidationRules    validationRules = new PropertyValidationRules(property, new[] { validationRule });

            return(property.SetMetadata <IProperty <T>, IPropertyValidationRules>(validationRules));
        }
        /// <summary>
        /// Sets property value if property is not set.
        /// </summary>
        /// <typeparam name="T">Property type.</typeparam>
        /// <param name="propertyContainer">Property container.</param>
        /// <param name="property">Property to set.</param>
        /// <param name="value">Value to set.</param>
        public static void SetValueIfNotSet <T>(this IMutablePropertyContainer propertyContainer, IProperty <T> property, [AllowNull] T value)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));
            property.AssertArgumentNotNull(nameof(property));

            var propertyValue = propertyContainer.GetPropertyValueUntyped(property, SearchOptions.ExistingOnly);

            if (propertyValue.IsNullOrNotDefined())
            {
                propertyContainer.SetValue(property, value !);
            }
        }
コード例 #7
0
        /// <inheritdoc />
        public IPropertyValue <T>?RemoveValue <T>(IProperty <T> property)
        {
            property.AssertArgumentNotNull(nameof(property));

            IPropertyValue?propertyValue = this.GetPropertyValueUntyped(property);

            if (propertyValue != null)
            {
                _propertyValues.Remove(propertyValue);
            }

            return((IPropertyValue <T>?)propertyValue);
        }
コード例 #8
0
        /// <summary>
        /// Adds <see cref="IPropertyValidationRule{T}"/> to metadata <see cref="IPropertyValidationRules"/>.
        /// </summary>
        /// <typeparam name="T">Property value type.</typeparam>
        /// <param name="property">Source property.</param>
        /// <param name="validation">Property validation.</param>
        /// <returns>The same property.</returns>
        public static IProperty <T> AddValidation <T>(this IProperty <T> property, Func <IProperty <T>, IPropertyValidationRule <T> > validation)
        {
            property.AssertArgumentNotNull(nameof(property));
            validation.AssertArgumentNotNull(nameof(validation));

            return(property.ConfigureMetadata <IProperty <T>, IPropertyValidationRules>(
                       createMetadata: CreatePropertyValidationRules,
                       configureMetadata: propertyValidation =>
            {
                IPropertyValidationRule <T> validationRule = validation(property);
                return propertyValidation.AddRule(validationRule);
            }));
        }
コード例 #9
0
        /// <summary>
        /// Gets property and value for untyped property using search conditions.
        /// Uses simple untyped search `SearchPropertyValueUntyped` if CanUseSimpleUntypedSearch or `property` has type <see cref="Search.UntypedSearch"/>.
        /// Uses full `GetPropertyValue{T}` based on property.Type in other cases.
        /// </summary>
        /// <param name="propertyContainer">Property container.</param>
        /// <param name="property">Property to search.</param>
        /// <param name="search">Search conditions.</param>
        /// <returns><see cref="IPropertyValue"/> or null.</returns>
        public static IPropertyValue?GetPropertyValueUntyped(
            this IPropertyContainer propertyContainer,
            IProperty property,
            SearchOptions?search = default)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));
            property.AssertArgumentNotNull(nameof(property));

            if ((search ?? propertyContainer.SearchOptions).CanUseSimpleUntypedSearch() || property.Type == typeof(Search.UntypedSearch))
            {
                return(propertyContainer.SearchPropertyValueUntyped(property, search));
            }

            return(GetPropertyValueUntypedFull(propertyContainer, property, search));
コード例 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Exists{T}"/> class.
 /// </summary>
 /// <param name="property">Property to check.</param>
 public Exists(IProperty <T> property)
 {
     Property = property.AssertArgumentNotNull(nameof(property));
     this.SetDefaultMessageFormat("{propertyName} is not exists.");
 }
コード例 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyValue{T}"/> class.
 /// </summary>
 /// <param name="property">Source property.</param>
 /// <param name="value">Value for property.</param>
 /// <param name="source">The source for value.</param>
 public PropertyValue(IProperty <T> property, T value, ValueSource source = null)
 {
     Property = property.AssertArgumentNotNull(nameof(property));
     Value    = value;
     Source   = source ?? ValueSource.Defined;
 }
コード例 #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyMapper{TSource, TTarget}"/> class.
 /// </summary>
 /// <param name="sourceProperty">Source property.</param>
 /// <param name="targetProperty">Target property.</param>
 /// <param name="valueMapper">Value mapper.</param>
 public PropertyMapper(IProperty <TSource> sourceProperty, IProperty <TTarget> targetProperty, IValueMapper <TSource, TTarget> valueMapper)
 {
     SourceProperty = sourceProperty.AssertArgumentNotNull(nameof(sourceProperty));
     TargetProperty = targetProperty.AssertArgumentNotNull(nameof(targetProperty));
     ValueMapper    = valueMapper.AssertArgumentNotNull(nameof(valueMapper));
 }
コード例 #13
0
        /// <inheritdoc/>
        public IPropertyValue CreateUntyped(IProperty property, object?value, ValueSource?valueSource = null)
        {
            property.AssertArgumentNotNull(nameof(property));

            return(_propertyValuesCache.GetOrAdd(new PropertyValueInfo(property, value, valueSource, _propertyValueFactory), propertyValueInfo => CreatePropertyValueUntyped(propertyValueInfo)));
        }
コード例 #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Required{T}"/> class.
 /// </summary>
 /// <param name="property">Property to check.</param>
 public Required(IProperty <T> property)
 {
     Property = property.AssertArgumentNotNull(nameof(property));
 }
コード例 #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyValidationRules"/> class.
 /// </summary>
 /// <param name="property">Property.</param>
 /// <param name="validationRules">Validation rules for property.</param>
 public PropertyValidationRules(IProperty property, IReadOnlyCollection <IValidationRule>?validationRules = null)
 {
     Property = property.AssertArgumentNotNull(nameof(property));
     Rules    = validationRules ?? Array.Empty <IValidationRule>();
 }
コード例 #16
0
 /// <summary>
 /// Gets validation rules attached to property.
 /// Rules are stored in <see cref="IPropertyValidationRules"/> metadata.
 /// </summary>
 /// <param name="property">Source property.</param>
 /// <returns>Validation rules.</returns>
 public static IEnumerable <IValidationRule> GetValidationRules(this IProperty property)
 {
     property.AssertArgumentNotNull(nameof(property));
     return(property.GetSchemaMetadata <IPropertyValidationRules>()?.Rules ?? Array.Empty <IValidationRule>());
 }
コード例 #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyValidationRule{T}"/> class.
 /// </summary>
 /// <param name="property">Property to check.</param>
 /// <param name="defaultMessageFormat">Default message format for validation message.</param>
 protected PropertyValidationRule(IProperty <T> property, string?defaultMessageFormat = null)
 {
     Property = property.AssertArgumentNotNull(nameof(property));
     this.SetDefaultMessageFormat(defaultMessageFormat);
 }
コード例 #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BasePropertyRule{T}"/> class.
 /// </summary>
 /// <param name="property">Property to check.</param>
 /// <param name="defaultMessageFormat">Default message format for validation message.</param>
 protected BasePropertyRule(IProperty <T> property, string defaultMessageFormat)
 {
     Property = property.AssertArgumentNotNull(nameof(property));
     this.SetDefaultMessageFormat(defaultMessageFormat);
 }
コード例 #19
0
        /// <inheritdoc />
        public IPropertyValue <T> Create <T>(IProperty <T> property, T?value, ValueSource?valueSource = null)
        {
            property.AssertArgumentNotNull(nameof(property));

            return((IPropertyValue <T>)_propertyValuesCache.GetOrAdd(new PropertyValueInfo(property, value, valueSource, _propertyValueFactory), propertyValueInfo => CreatePropertyValue <T>(propertyValueInfo)));
        }