/// <summary>
        /// Validates <paramref name="propertyContainer"/> against validation rules from <paramref name="validator"/>.
        /// </summary>
        /// <param name="validator">Validator that provides validation rules to check.</param>
        /// <param name="propertyContainer"><see cref="IPropertyContainer"/> to validate.</param>
        /// <returns>Validation messages.</returns>
        public static IEnumerable <Message> Validate(this IValidator validator, IPropertyContainer propertyContainer)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));
            validator.AssertArgumentNotNull(nameof(validator));

            return(propertyContainer.Validate(validator.GetRules()));
        }
        /// <summary>
        /// Clones source <paramref name="propertyContainer"/> as <see cref="PropertyContainer"/>.
        /// </summary>
        /// <param name="propertyContainer">Source property container.</param>
        /// <returns>ReadOnly copy of initial property container.</returns>
        public static IPropertyContainer CloneAsReadOnly(this IPropertyContainer propertyContainer)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));

            return(new PropertyContainer(
                       sourceValues: propertyContainer.Properties,
                       parentPropertySource: propertyContainer.ParentSource,
                       searchOptions: propertyContainer.SearchOptions));
        }
        /// <summary>
        /// Merges <see cref="IPropertyContainer"/> objects to single <see cref="IPropertyContainer"/>.
        /// </summary>
        /// <param name="propertyContainer">Source property container.</param>
        /// <param name="otherContainer">Container to merge to source container.</param>
        /// <param name="mergeMode">Merge mode. Default: <see cref="PropertyAddMode.Set"/>.</param>
        /// <returns>New <see cref="IPropertyContainer"/> instance.</returns>
        public static IPropertyContainer Merge(
            this IPropertyContainer propertyContainer,
            IPropertyContainer otherContainer,
            PropertyAddMode mergeMode = PropertyAddMode.Set)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));
            otherContainer.AssertArgumentNotNull(nameof(otherContainer));

            return(propertyContainer.ToMutable().WithValues(otherContainer, mergeMode));
        }
        /// <summary>
        /// Merges composite properties to one container.
        /// </summary>
        /// <param name="propertyContainer">Source property container.</param>
        /// <param name="mergeMode">Merge mode. Default: <see cref="PropertyAddMode.Set"/>.</param>
        /// <param name="properties">Properties to merge.</param>
        /// <returns>New <see cref="IPropertyContainer"/> instance.</returns>
        public static IPropertyContainer MergeProperties(
            this IPropertyContainer propertyContainer,
            PropertyAddMode mergeMode = PropertyAddMode.Set,
            params IProperty <IPropertyContainer>[]?properties)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));

            return(properties
                   .NotNull()
                   .Select(property => propertyContainer.GetValue(property))
                   .Merge(mergeMode));
        }
        /// <summary>
        /// Validates <paramref name="propertyContainer"/> against validation rules from <paramref name="validationRules"/>.
        /// </summary>
        /// <param name="propertyContainer"><see cref="IPropertyContainer"/> to validate.</param>
        /// <param name="validationRules">Validation rules to check.</param>
        /// <returns>Validation messages.</returns>
        public static IEnumerable <Message> Validate(this IPropertyContainer propertyContainer, IEnumerable <IValidationRule> validationRules)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));
            validationRules.AssertArgumentNotNull(nameof(validationRules));

            foreach (var rule in validationRules)
            {
                foreach (Message message in rule.Validate(propertyContainer))
                {
                    yield return(message);
                }
            }
        }
        /// <summary>
        /// Converts to <see cref="IMutablePropertyContainer"/> if needed.
        /// Returns the same container if it can be casted to <see cref="IMutablePropertyContainer"/>
        /// Or returns new <see cref="MutablePropertyContainer"/> copy of <paramref name="propertyContainer"/>.
        /// </summary>
        /// <param name="propertyContainer">Source property container.</param>
        /// <returns><see cref="IMutablePropertyContainer"/>.</returns>
        public static IMutablePropertyContainer ToMutable(this IPropertyContainer propertyContainer)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));

            if (propertyContainer is IMutablePropertyContainer mutablePropertyContainer)
            {
                return(mutablePropertyContainer);
            }

            return(new MutablePropertyContainer(
                       sourceValues: propertyContainer.Properties,
                       parentPropertySource: propertyContainer.ParentSource,
                       searchOptions: propertyContainer.SearchOptions));
        }
Esempio n. 7
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));
        /// <summary>
        /// Flattens container hierarchy to single container (from oldest to current).
        /// </summary>
        /// <param name="propertyContainer">Source container.</param>
        /// <returns>New container.</returns>
        public static IPropertyContainer Flatten(this IPropertyContainer propertyContainer)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));

            if (propertyContainer.ParentSource != null && propertyContainer.ParentSource.Count > 0)
            {
                var merger = new MutablePropertyContainer(searchOptions: propertyContainer.SearchOptions);

                var hierarchy = GetHierarchy(propertyContainer);
                foreach (IPropertyContainer ancestor in hierarchy)
                {
                    merger.WithValues(ancestor, PropertyAddMode.Set);
                }

                return(merger);
            }

            return(propertyContainer);
        }
        /// <summary>
        /// Converts to read only <see cref="IPropertyContainer"/> if needed.
        /// Returns read only copy of <paramref name="propertyContainer"/> if it is <see cref="IMutablePropertyContainer"/>.
        /// Or returns the same container.
        /// </summary>
        /// <param name="propertyContainer">Source property container.</param>
        /// <param name="flattenHierarchy">Flatten container hierarchy.</param>
        /// <returns><see cref="IPropertyContainer"/>.</returns>
        public static IPropertyContainer ToReadOnly(this IPropertyContainer propertyContainer, bool flattenHierarchy = true)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));

            if (propertyContainer is IMutablePropertyContainer)
            {
                if (flattenHierarchy && propertyContainer.ParentSource != null && propertyContainer.ParentSource.Count > 0)
                {
                    return(propertyContainer
                           .Flatten()
                           .ToReadOnly(flattenHierarchy: false));
                }

                return(new PropertyContainer(
                           sourceValues: propertyContainer.Properties,
                           parentPropertySource: propertyContainer.ParentSource,
                           searchOptions: propertyContainer.SearchOptions));
            }

            return(propertyContainer);
        }
        /// <summary>
        /// Gets optional <see cref="IObjectSchema"/> from <see cref="IHasSchema"/> metadata.
        /// </summary>
        /// <param name="propertyContainer">Source property container.</param>
        /// <returns>Optional <see cref="IMutableObjectSchema"/>.</returns>
        public static IObjectSchema?GetSchema(this IPropertyContainer propertyContainer, bool autoCreateByProperties = false)
        {
            propertyContainer.AssertArgumentNotNull(nameof(propertyContainer));

            if (propertyContainer.GetHasSchema() is { Schema : { } schema })
Esempio n. 11
0
 public static IPropertyContainer CreateHierarchicalContainer(
     IPropertyContainer propertyContainer1,
     IPropertyContainer?propertyContainer2,
     bool mergeHierarchy = true)
 {
     propertyContainer1.AssertArgumentNotNull(nameof(propertyContainer1));
Esempio n. 12
0
        public HierarchicalContainer(IPropertyContainer propertyContainer1, IPropertyContainer?propertyContainer2)
        {
            propertyContainer1.AssertArgumentNotNull(nameof(propertyContainer1));

            _propertyContainer = PropertyContainer.CreateHierarchicalContainer(propertyContainer1, propertyContainer2);
        }