Ejemplo n.º 1
0
        private Operation CreateOperationFromAttribute(
            XElement element,
            ElementKind kind,
            XAttribute attribute,
            bool throwOnId = false)
        {
            string attributeName = attribute.LocalName();

            if (throwOnId &&
                DefaultComparer.NameEquals(attributeName, AttributeNames.Id))
            {
                Throw(ErrorMessages.CannotUseOperationOnProperty(element, attributeName));
            }

            PropertyDefinition property;

            string name = attribute.LocalName();

            if (name == PropertyDefinition.TagsName)
            {
                property = PropertyDefinition.Tags;
            }
            else
            {
                property = GetProperty(attribute);
            }

            switch (kind)
            {
            case ElementKind.With:
            {
                return(new Operation(property, GetValue(attribute), _depth, OperationKind.With));
            }

            case ElementKind.Without:
            {
                if (!property.IsCollection)
                {
                    Throw(ErrorMessages.CannotUseOperationOnNonCollectionProperty(element, property.Name));
                }

                return(new Operation(property, GetValue(attribute), _depth, OperationKind.Without));
            }

            default:
            {
                Debug.Assert(kind == ElementKind.New, kind.ToString());

                return(new Operation(property, GetValue(attribute), _depth, OperationKind.With));
            }
            }
        }
Ejemplo n.º 2
0
        private IPropertyOperation CreateOperationFromAttribute(
            XElement element,
            ElementKind kind,
            XAttribute attribute,
            char separator         = ',',
            bool throwOnId         = false,
            bool throwOnCollection = false)
        {
            string attributeName = attribute.LocalName();

            if (throwOnId &&
                DefaultComparer.NameEquals(attributeName, AttributeNames.Id))
            {
                Throw(ErrorMessages.CannotUseOperationOnProperty(element, attributeName));
            }

            PropertyDefinition property;

            string name = attribute.LocalName();

            if (name == PropertyDefinition.TagsName)
            {
                property = PropertyDefinition.Tags;
            }
            else
            {
                property = GetProperty(attribute);
            }

            if (throwOnCollection &&
                property.IsCollection)
            {
                Throw(ErrorMessages.CannotUseOperationOnCollectionProperty(element, property.Name));
            }

            switch (kind)
            {
            case ElementKind.Add:
                return(new AddOperation(property, GetValue(attribute), Depth));

            case ElementKind.AddRange:
                return(new AddRangeOperation(property, GetValue(attribute), separator, Depth));

            case ElementKind.Remove:
                return(new RemoveOperation(property, GetValue(attribute), Depth));

            case ElementKind.RemoveRange:
                return(new RemoveRangeOperation(property, GetValue(attribute), separator, Depth));

            default:
            {
                Debug.Assert(kind == ElementKind.Set || kind == ElementKind.New, kind.ToString());

                if (property.IsCollection)
                {
                    return(new AddOperation(property, GetValue(attribute), Depth));
                }
                else
                {
                    return(new SetOperation(property, GetValue(attribute), Depth));
                }
            }
            }
        }