コード例 #1
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var content = source as IPublishedContent;

            if (content == null)
            {
                throw new Exception("Expected source type IPublishedContent");
            }

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            var componentModel = new T();

            //If component was not mapped inline, then we need to fetch it from the mapping registry
            if (_componentMapping == null)
            {
                _componentMapping = Populate.GetMappingForType(typeof(T));

                if (_componentMapping == null)
                {
                    throw new Exception($"No component mapper is defined for type: {typeof(T).FullName}");
                }
            }

            var rules = _componentMapping.GetRules();

            foreach (var rule in rules)
            {
                rule.Execute(session, options, componentModel, typeof(T), content);
            }

            destProperty.SetValue(model, componentModel);
        }
コード例 #2
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var content = source as IPublishedContent;

            if (content == null)
            {
                throw new Exception("Expected source type IPublishedContent");
            }

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            var contentValue = _sourceProperty(content);

            if (destProperty.PropertyType.IsInstanceOfType(contentValue))
            {
                destProperty.SetValue(model, contentValue);
            }

            else
            {
                var convert = contentValue.TryConvertTo(destProperty.PropertyType);

                if (convert.Success)
                {
                    destProperty.SetValue(model, convert.Result);
                }
            }
        }
コード例 #3
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var content = source as IPublishedContent;

            if (content == null)
            {
                throw new Exception("Expected source type IPublishedContent");
            }

            if (_isLazy && (!options.IncludedProperties.ContainsKey(type) || !options.IncludedProperties[type].Contains(_propertyName)))
            {
                return;
            }

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            //set to empty list.  This will prevent loading the list again if the nested session.Map<> contains a circular reference
            destProperty.SetValue(model, new List <TModel>());

            var filtered = _filter(content);

            var collection = session.Map <TModel>(filtered).WithOptions(options).List();

            destProperty.SetValue(model, collection);
        }
コード例 #4
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            var fieldset = source as ArchetypeFieldsetModel;

            if (fieldset == null)
            {
                return;
            }

            if (!fieldset.HasProperty(_propertyAlias) || !fieldset.HasValue(_propertyAlias))
            {
                return;
            }

            var srcValue = fieldset.GetValue <string>(_propertyAlias);

            var helper = new UmbracoHelper(UmbracoContext.Current);

            var node = _isMedia ? helper.TypedMedia(srcValue) : helper.TypedContent(srcValue);

            if (node == null)
            {
                //Orphan node reference, so ignore
                return;
            }

            var mappedNode = session.Map <TModel>(node).WithOptions(options).Single();

            destProperty.SetValue(model, mappedNode);
        }
コード例 #5
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var relatedContent = session.Map <TModel>(_filter(new UmbracoHelper(UmbracoContext.Current))).WithOptions(options).Single();

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            destProperty.SetValue(model, relatedContent);
        }
コード例 #6
0
 public ArchetypeMappingExecutor(IMappingSession session, ArchetypeModel model, string dataType)
 {
     _session  = session;
     _model    = model;
     _type     = typeof(T);
     _results  = new List <T>();
     _options  = new MappingOptions();
     _dataType = dataType;
 }
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            if (_isLazy && (!options.IncludedProperties.ContainsKey(type) || !options.IncludedProperties[type].Contains(_propertyName)))
            {
                return;
            }

            var filtered = _filter(new UmbracoHelper(UmbracoContext.Current));

            var collection = session.Map <TModel>(filtered).WithOptions(options).List();

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            destProperty.SetValue(model, collection);
        }
コード例 #8
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var content = source as IPublishedContent;

            if (content == null)
            {
                throw new Exception("Expected source type IPublishedContent");
            }

            var relatedContent = session.Map <TModel>(_filter(content)).WithOptions(options).Single();

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            destProperty.SetValue(model, relatedContent);
        }
コード例 #9
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var dataTypeService = UmbracoContext.Current.Application.Services.DataTypeService;
            var destProperty    = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            ArchetypeModel      srcValue;
            IDataTypeDefinition dataTypeDefinition;

            if (source is ArchetypeFieldsetModel)
            {
                var fieldSet = source as ArchetypeFieldsetModel;
                var property = fieldSet.Properties.FirstOrDefault(x => x.Alias == _propertyAlias);

                srcValue           = fieldSet.GetValue <ArchetypeModel>(_propertyAlias);
                dataTypeDefinition = dataTypeService.GetDataTypeDefinitionById(property.DataTypeId);
            }

            else
            {
                var content     = source as IPublishedContent;
                var srcProperty = content.GetProperty(_propertyAlias);

                if (srcProperty == null)
                {
                    return;
                }

                var propertyType = content.ContentType.GetPropertyType(_propertyAlias);

                dataTypeDefinition = dataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeId);

                srcValue = srcProperty.Value as ArchetypeModel;
            }

            if (srcValue == null)
            {
                return;
            }

            if (!srcValue.Any())
            {
                return;
            }

            var archetypeModel = session.Map <T>(srcValue, dataTypeDefinition.Name).Single();

            destProperty.SetValue(model, archetypeModel);
        }
コード例 #10
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var content = source as IPublishedContent;

            if (content == null)
            {
                throw new Exception("Expected source type IPublishedContent");
            }

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            var srcProperty = content.GetProperty(_propertyAlias);

            if ((srcProperty == null) || !srcProperty.HasValue)
            {
                return;
            }

            var srcValue = srcProperty.Value;

            var helper = new UmbracoHelper(UmbracoContext.Current);

            try
            {
                var node = _isMedia ? helper.TypedMedia(srcValue) : helper.TypedContent(srcValue);

                if (node == null)
                {
                    //Orphan node reference, so ignore
                    return;
                }

                var mappedNode = session.Map <TModel>(node).WithOptions(options).Single();

                destProperty.SetValue(model, mappedNode);
            }

            catch
            {
                //suppress error The valueDictionary is not formatted correctly and is missing any of the  'id,nodeId,__NodeId' elements until fix in 7.2
            }
        }
コード例 #11
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var content = source as IPublishedContent;

            if (content == null)
            {
                throw new Exception("Expected source type IPublishedContent");
            }

            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            var srcValue = content.GetPropertyValue <T>(_propertyAlias);

            if (_transform != null)
            {
                srcValue = _transform(srcValue);
            }

            destProperty.SetValue(model, srcValue);
        }
コード例 #12
0
        void IMappingRule.Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            var fieldset = source as ArchetypeFieldsetModel;

            if (fieldset == null)
            {
                return;
            }

            if (!fieldset.HasProperty(_propertyAlias) || !fieldset.HasValue(_propertyAlias))
            {
                return;
            }

            var srcValue = fieldset.GetValue <T>(_propertyAlias);

            destProperty.SetValue(model, srcValue);
        }
コード例 #13
0
        public void Execute(IMappingSession session, MappingOptions options, object model, Type type, object source)
        {
            var destProperty = type.GetProperty(_propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? type.GetProperty(_propertyName);

            var content = source as IPublishedContent;

            if (content == null)
            {
                return;
            }

            if (_isLazy && (!options.IncludedProperties.ContainsKey(type) || !options.IncludedProperties[type].Contains(_propertyName)))
            {
                return;
            }

            var srcProperty = content.GetProperty(_propertyAlias);

            if ((srcProperty == null) || !srcProperty.HasValue)
            {
                return;
            }

            var srcValue = srcProperty.Value as string;

            var srcNodes = srcValue.Split(',');

            var helper = new UmbracoHelper(UmbracoContext.Current);

            var list = srcNodes.Select(x =>
            {
                var node = _isMedia ? helper.TypedMedia(x) : helper.TypedContent(x);

                return(node == null ? null : session.Map <TModel>(node).WithOptions(options).Single());
            }).Where(n => n != null).ToList();

            destProperty.SetValue(model, list);
        }
コード例 #14
0
 public static ArchetypeMappingExecutor <T> Map <T>(this IMappingSession session, ArchetypeModel model, string dataType) where T : class
 {
     return(new ArchetypeMappingExecutor <T>(session, model, dataType));
 }