/// <summary>
        /// Extension used to convert an IPublishedContent back to a Typed model instance.
        /// Your model does need to inherit from UmbracoGeneratedBase and contain the correct attributes
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="content"></param>
        /// <returns></returns>
        public static T ConvertToModel <T>(this IPublishedContent content)
        {
            T   instance = Activator.CreateInstance <T>();
            var propertiesWithTabsAttribute = typeof(T).GetProperties().Where(x => x.GetCustomAttribute <UmbracoTabAttribute>() != null);

            foreach (var property in propertiesWithTabsAttribute)
            {
                //tab instance
                UmbracoTabAttribute tabAttribute = property.GetCustomAttribute <UmbracoTabAttribute>();
                var propertyTabInstance          = Activator.CreateInstance(property.PropertyType);
                var propertiesOnTab = property.PropertyType.GetProperties();

                foreach (var propertyOnTab in propertiesOnTab.Where(x => x.GetCustomAttribute <UmbracoPropertyAttribute>() != null))
                {
                    UmbracoPropertyAttribute propertyAttribute = propertyOnTab.GetCustomAttribute <UmbracoPropertyAttribute>();
                    string alias = HyphenToUnderscore(ParseUrl(propertyAttribute.Alias + "_" + tabAttribute.Name, false));
                    GetPropertyValueOnInstance(content, propertyTabInstance, propertyOnTab, propertyAttribute, alias);
                }

                property.SetValue(instance, propertyTabInstance);
            }

            //properties on Generic Tab
            var propertiesOnGenericTab = typeof(T).GetProperties().Where(x => x.GetCustomAttribute <UmbracoPropertyAttribute>() != null);

            foreach (var item in propertiesOnGenericTab)
            {
                UmbracoPropertyAttribute umbracoPropertyAttribute = item.GetCustomAttribute <UmbracoPropertyAttribute>();
                GetPropertyValueOnInstance(content, instance, item, umbracoPropertyAttribute);
            }

            (instance as UmbracoGeneratedBase).UmbracoId = content.Id;
            return(instance);
        }
Пример #2
0
        /// <summary>
        /// Abstract base class for any entity you want to be generated
        /// The persist method makes sure that you can save all the changes to you entity back into the database
        /// </summary>
        /// <param name="contentId">Id of the Umbraco Document</param>
        /// <param name="userId"></param>
        /// <param name="raiseEvents"></param>
        public virtual void Persist(int contentId, int userId = 0, bool raiseEvents = false)
        {
            IContentService contentSerivce = ApplicationContext.Current.Services.ContentService;
            IContent        content        = contentSerivce.GetById(contentId);

            //search for propertys with the UmbracoTab on
            Type currentType = this.GetType();
            var  propertiesWithTabAttribute = currentType.GetProperties().Where(x => x.GetCustomAttributes <UmbracoTabAttribute>() != null).ToArray();
            int  length = propertiesWithTabAttribute.Count();

            for (int i = 0; i < length; i++)
            {
                PropertyInfo        tabProperty   = propertiesWithTabAttribute[i];
                Type                tabType       = tabProperty.PropertyType;
                object              instanceOfTab = tabProperty.GetValue(this);
                UmbracoTabAttribute tabAttribute  = tabProperty.GetCustomAttribute <UmbracoTabAttribute>();

                //persist the fields foreach tab
                var propertiesInsideTab = tabType.GetProperties().Where(x => x.GetCustomAttribute <UmbracoPropertyAttribute>() != null).ToArray();
                int propertyLength      = propertiesInsideTab.Length;
                for (int j = 0; j < propertyLength; j++)
                {
                    PropertyInfo             property = propertiesInsideTab[j];
                    UmbracoPropertyAttribute umbracoPropertyAttribute = property.GetCustomAttribute <UmbracoPropertyAttribute>();
                    object propertyValue = property.GetValue(instanceOfTab);
                    string alias         = UmbracoCodeFirstExtensions.HyphenToUnderscore(UmbracoCodeFirstExtensions.ParseUrl(umbracoPropertyAttribute.Alias + "_" + tabAttribute.Name, false));
                    SetPropertyOnIContent(content, umbracoPropertyAttribute, propertyValue, alias);
                }
            }

            //properties on generic tab
            var propertiesOnGenericTab = currentType.GetProperties().Where(x => x.GetCustomAttribute <UmbracoPropertyAttribute>() != null);

            foreach (var item in propertiesOnGenericTab)
            {
                UmbracoPropertyAttribute umbracoPropertyAttribute = item.GetCustomAttribute <UmbracoPropertyAttribute>();
                object propertyValue = item.GetValue(this);
                SetPropertyOnIContent(content, umbracoPropertyAttribute, propertyValue);
            }

            //persist object into umbraco database
            contentSerivce.SaveAndPublishWithStatus(content, userId, raiseEvents);
        }