Пример #1
0
        /// <summary>
        /// Gets the default processor for the given object type.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <returns>
        /// Returns the default processor for the given object type.
        /// </returns>
        public DittoProcessorAttribute GetDefaultProcessorFor(Type objectType)
        {
            if (Ditto.TryGetTypeAttribute(objectType, out DittoDefaultProcessorAttribute attr))
            {
                return(attr.ProcessorType.GetInstance <DittoProcessorAttribute>());
            }

            return(DefaultProcessorType.GetInstance <DittoProcessorAttribute>());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DittoProcessorAttribute"/> class.
        /// </summary>
        protected DittoProcessorAttribute()
        {
            if (Ditto.TryGetTypeAttribute(this.GetType(), out DittoProcessorMetaDataAttribute metaData, true) == false || metaData == null)
            {
                throw new ApplicationException($"The Ditto processor attribute ('{this.GetType()}') requires a DittoProcessorMetaData attribute to be applied to the class but none was found.");
            }

            this.ValueType   = metaData.ValueType;
            this.ContextType = metaData.ContextType;
        }
Пример #3
0
        /// <summary>
        /// Processes the value.
        /// </summary>
        /// <returns>
        /// The <see cref="object" /> representing the processed value.
        /// </returns>
        public override object ProcessValue()
        {
            var defaultValue = this.DefaultValue;

            var recursive   = this.Recursive;
            var propName    = this.Context.PropertyInfo != null ? this.Context.PropertyInfo.Name : string.Empty;
            var altPropName = string.Empty;

            // Check for Umbraco properties attribute on class
            if (Ditto.TryGetTypeAttribute(this.Context.TargetType, out UmbracoPropertiesAttribute classAttr))
            {
                // Apply the prefix
                if (string.IsNullOrWhiteSpace(classAttr.Prefix) == false)
                {
                    altPropName = propName;
                    propName    = classAttr.Prefix + propName;
                }

                // Apply global recursive setting
                recursive |= classAttr.Recursive;

                // Apply property source only if it's different from the default,
                // and the current value is the default. We only do it this
                // way because if they change it at the property level, we
                // want that to take precedence over the class level.
                if (classAttr.PropertySource != Ditto.DefaultPropertySource && PropertySource == Ditto.DefaultPropertySource)
                {
                    PropertySource = classAttr.PropertySource;
                }
            }

            var umbracoPropertyName    = this.PropertyName ?? propName;
            var altUmbracoPropertyName = this.AltPropertyName ?? altPropName;

            var content = this.Value as IPublishedContent;

            if (content == null)
            {
                return(defaultValue);
            }

            object propertyValue = null;

            // Try fetching the value.
            if (string.IsNullOrWhiteSpace(umbracoPropertyName) == false)
            {
                propertyValue = GetPropertyValue(content, umbracoPropertyName, recursive);
            }

            // Try fetching the alt value.
            if ((propertyValue == null || (propertyValue is string tmp && string.IsNullOrWhiteSpace(tmp))) &&
                string.IsNullOrWhiteSpace(altUmbracoPropertyName) == false)
            {
                propertyValue = GetPropertyValue(content, altUmbracoPropertyName, recursive);
            }

            // Try setting the default value.
            if ((propertyValue == null || (propertyValue is string tmp2 && string.IsNullOrWhiteSpace(tmp2))) &&
                defaultValue != null)
            {
                propertyValue = defaultValue;
            }

            return(propertyValue);
        }